Download presentation
Presentation is loading. Please wait.
1
Lecture 11 Oct 14, 02
2
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.
3
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; }
4
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); }
5
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.
6
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
7
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; }
8
factorial iteratively ► factorial = 1; for( int count = num; count>=1; count--) for( int count = num; count>=1; count--) factorial *=count; factorial *=count;
9
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)
10
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= -4.23456 double c= -4.23456 cdabs(a); cdabs(a); cdabs(b); cdabs(b); cdabs(c); cdabs(c); return 0; return 0; }
11
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; }
12
► 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; }
13
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.
14
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; }
15
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; }
16
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
17
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
18
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
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.