recursion a function is said to be recursive if it calls itself, either directly or indirectly. void...
DESCRIPTION
Recursion contd. When writing a recursive function, there are two rules that need to be adhered to. – Every recursive function must have a base case, where the result is known – When a function invokes itself, the value is defined in terms of previously defined function values, and the value used for testing for the base case must change.TRANSCRIPT
![Page 1: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/1.jpg)
Recursion
A function is said to be recursive if it calls itself, either directly or indirectly.
void repeat( int n ) {cout << n << " ";repeat( --n );
}
![Page 2: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/2.jpg)
Recursion contd.
Simple recursive routines follow a standard pattern. Typically there is a base case ( or cases ) that is tested for, upon entry to the function. Then there is a general recursive case in which one of the variables, often an integer, is passed as an argument in such a way as to ultimately lead to the base case.
![Page 3: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/3.jpg)
Recursion contd.
When writing a recursive function, there are two rules that need to be adhered to.
– Every recursive function must have a base case, where the result is known
– When a function invokes itself, the value is defined in terms of previously defined function values, and the value used for testing for the base case must change.
![Page 4: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/4.jpg)
Recursion contd.
void repeat( int n ) {if( n<0 ) {
cout << "END" << endl;return;
}else {
cout << n << " ";repeat( --n );
}}
![Page 5: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/5.jpg)
Recursion Example
int sum( int n ){
if (n <= 1 )return n; // base caseelsereturn (n + sum( n -1 )); // current value is defined in terms of // a different value of the parameter
}sum(1) 1sum(2) 2 + sum(1) 2 + 1 sum(3) 3 + sum(2) 3 + 2 + 1
![Page 6: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/6.jpg)
Factorial Example
Let’s find the factorial of n where n is a non-negative integer.– Product of integers from 1 to n– If n = 0, then n! is 1
0! = 1 n! = n(n-1)… 3.2.1 for n > 0
int factorial( int n ){
if (n <= 1 )return 1;elsereturn ( n * factorial(n-1 ) );
}
What is the Big-Oh?
![Page 7: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/7.jpg)
Iterative Version of the Factorial Example
int factorial( int n ){
int value = 1;for ( int i=2, i<=n, i++ )
value *= i;return value;
}What is the Big-Oh for this?
![Page 8: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/8.jpg)
Pros and Cons of Recursion
Disadvantages:– Slow – time is taken for function call– Uses more memory – requires space for stack
frame
Advantages:– When it is a simple, elegant solution that is easy to
understand– When the number of recursive calls is a fraction of n
![Page 9: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/9.jpg)
When to Use Recursion
1. Iterative functions are typically faster than their recursive counterparts. So, if speed is an issue, you would normally use iteration.
2. If the stack limit is too constraining then you will prefer iteration over recursion.
3. Some procedures are very naturally programmed recursively, and all but unmanageable iteratively. Here, then, the choice is clear.
![Page 10: Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout](https://reader035.vdocuments.us/reader035/viewer/2022081804/5a4d1b4e7f8b9ab0599a6804/html5/thumbnails/10.jpg)
Example of When to Use Recursion
Algorithm Power( x, n ) Input: base x, exponent n where n 0 Output: value of xn if n = 0 then return 1if n is odd then y Power(x,(n-1)/2) return x * y * yelse y Power(x,n/2) return y * y
1 if n = 0power(x,n) x * power(x,(n-1)/2)2 if n>0 is odd
power(x,n/2)2 if n>0 is even