Lecture 11 Oct 14, 02. Recursion ► The programs we have discussed are generally structured as functions that call one another in a disciplined, hierarchical.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 5 Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
C++ for Engineers and Scientists Third Edition
Functions g g Data Flow g Scope local global part 4 part 4.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Functions g g Data Flow g Scope local global part II g Global Resolution Operator part II.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
CSCI 171 Presentation 6 Functions and Variable Scope.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Programming Fundamentals1 Chapter 6 FUNCTIONS AND POINTERS.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
C++ Programming Lecture 12 Functions – Part IV
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
A First Book of ANSI C Fourth Edition
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Chapter 6 Modularity Using Functions
Lecture 10 Oct 7, 02. Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The.
User Defined Functions
Introduction to Programming
-Neelima Singh PGT(CS) KV Sec-3 Rohini
IS Program Design and Software Tools Introduction to C++ Programming
C Functions -Continue…-.
Variables A piece of memory set aside to store data
Chapter 5 Functions DDC 2133 Programming II.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
CHAPTER 6 GENERAL-PURPOSE METHODS
Local Variables, Global Variables and Variable Scope
Unit 3 Test: Friday.
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

Lecture 11 Oct 14, 02

Recursion ► The programs we have discussed are generally structured as functions that call one another in a disciplined, hierarchical manner. ► A recursive function is a function that calls itself.

Example # 1 for Recursion ( Finding Factorial of a number ) ► #include ► #include unsigned long factorial(unsigned long); unsigned long factorial(unsigned long); // The above is the function prototype // The above is the function prototype int main() int main() { for (int i=0; i<=10; i++) for (int i=0; i<=10; i++) cout<<i<<“!=“<<factorial(i)<<endl; cout<<i<<“!=“<<factorial(i)<<endl; // function call “factorial(i)” done in cout // function call “factorial(i)” done in cout return 0; return 0; }

factorial() function ► unsigned long factorial( unsigned long number) { if (number<=1) // base case if (number<=1) // base case return 1; return 1; else // recursive case else // recursive case return number * factorial (number -1); return number * factorial (number -1); }

Explanation ► The function is first called from the main() function. ► The base case condition is checked: if it is true: if it is true: the return following base case is executed. the return following base case is executed. else: else: recursion case is executed. recursion case is executed. ► In the recursion case, the function calls itself.

Example 2 The fibonacci series ► #include ► #include /* no function prototype required because function is written before the main function */ /* no function prototype required because function is written before the main function */ unsigned long fibonacci (unsigned long n) unsigned long fibonacci (unsigned long n) { if ( n==0 || n==1) // || is or operator if ( n==0 || n==1) // || is or operator return n; return n; else else return fibonacci(n-1) + fibonacci(n-2); return fibonacci(n-1) + fibonacci(n-2); } // above recursive function call } // above recursive function call

main function() ► int main() { unsigned long result, number; unsigned long result, number; cout<<“Enter an integer: “; cout<<“Enter an integer: “; cin>>number; cin>>number; result = fibonacci(number); // first function call result = fibonacci(number); // first function call cout<<“Fibonacci(“<<number<<“)=<<result; cout<<“Fibonacci(“<<number<<“)=<<result; return 0; return 0; }

factorial iteratively ► factorial = 1; for( int count = num; count>=1; count--) for( int count = num; count>=1; count--) factorial *=count; factorial *=count;

function overloading ► C++ provides the capability of using the same function for more than one function. This is called function overloading. ► The only requirement in creating more than one function with the same name is that the compiler must be able to determine which function to use based on the data type of the parameters (not the data type of the return vale, if any)

Example # 3 (function overloading) function main() ► #include ► #include int main() int main() { int a=-10; float b=- 6.28; int a=-10; float b=- 6.28; double c= double c= cdabs(a); cdabs(a); cdabs(b); cdabs(b); cdabs(c); cdabs(c); return 0; return 0; }

The different functions ► void cdabs( int x ) { if (x<0) if (x<0) x=-x; x=-x; cout<<“abs value is”<<x<<endl; cout<<“abs value is”<<x<<endl; } void cdabs( float x) void cdabs( float x) { if (x<0) if (x<0) x=-x; x=-x; cout<<“abs value is”<<x<<endl; cout<<“abs value is”<<x<<endl; }

► void cdabs(double x) { if (x<0) if (x<0) x=-x; x=-x; cout<<“Abs value is:”<<x<<endl; cout<<“Abs value is:”<<x<<endl; }

Variable scope ► Scope is defined as the section of the program where the variable is valid or known. ► There are two scopes for variables: 1 – local 1 – local 2 – global 2 – global ► Variables created inside a function are conventionally available only to the function itself, they are said to be local to the function or local variables. ► A global variable, is one whose storage has been created for it by a declaration statement located outside any function. These variables can be used by all functions that are physically placed after the global variable declaration.

Example # 4 Scope of variables ► #include ► #include int firstnum // global variable declaration int firstnum // global variable declaration void valfun(void) // function prototype declaration void valfun(void) // function prototype declaration int main() int main() { int secnum; // local variable declaration int secnum; // local variable declaration firstnum=10; firstnum=10; sec num = 20; sec num = 20; cout<<“From main(): firstnum =“<<firstnum<<endl; cout<<“From main(): firstnum =“<<firstnum<<endl; cout<<“From main(): secnum =“<<secnum<<endl; cout<<“From main(): secnum =“<<secnum<<endl; valfun(); // function call valfun(); // function call cout<<“From main() again: firstnum=“<<firstnum<<endl; cout<<“From main() again: firstnum=“<<firstnum<<endl; return 0; return 0; }

The valfun() function ► void valfun(void) // function header { int secnum; // create a second local variable named secnum int secnum; // create a second local variable named secnum secum=30; secum=30; cout<<“From valfun(): firstnum=“ <<firstnum<<endl; cout<<“From valfun(): firstnum=“ <<firstnum<<endl; cout<<“From valfun(): secnum=“ <<secnum<<endl; cout<<“From valfun(): secnum=“ <<secnum<<endl; firstnum = 40; // this changes first num for both functions firstnum = 40; // this changes first num for both functions return; return; }

Output example # 4 ► From main(): firstnum = 10 From main(): secnum = 20 From main(): secnum = 20 From valfun(): firstnum = 10 From valfun(): firstnum = 10 From valfun(): secnum = 30 From valfun(): secnum = 30 From main() again: firstnum = 40 From main() again: firstnum = 40 From main() again: secnum = 20 From main() again: secnum = 20

Imp note ► When a local variable name has the same name as global variable, local variable name takes precedence over global variable #include #include float num = 42.8; //global variable named num float num = 42.8; //global variable named num int main() int main() { float num = 26.4; // local variable named num float num = 26.4; // local variable named num cout<<“ The value of num is “<<num<<endl; cout<<“ The value of num is “<<num<<endl; return 0; return 0; } output : The value of num is 26.4 output : The value of num is 26.4

Scope Resolution Operator ► When a local variable has the same name as global variable, we have seen all references to the variable name made within the scope of the local variable refer to the local variable. ► In such cases, we can still access the global varible by using C++’s scope resolution operator. ► This operator, which has the symbol :: must be placed before the variable name as in ::number

Example # 5 (Scope Resolution Operator) ► #include ► #include float num = 42.5; // global variable named num float num = 42.5; // global variable named num int main() int main() { float num = 26.4; // local variable named num float num = 26.4; // local variable named num cout<<“The value of num is “<<::num<<endl; cout<<“The value of num is “<<::num<<endl; return 0; return 0; } output : The value of num is 42.5 output : The value of num is 42.5