Download presentation
Presentation is loading. Please wait.
Published byHelen Chandler Modified over 9 years ago
1
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 6, Recursion Thursday 1 July 2010
2
Overview Recursion Implementing a solution through recursive function calls Normal execution of a function Recursive execution Computational cost of recursion Need, definition and usage Lecture 6 Recursion
3
Iteration Vs Recursion We have seen some problems which implement the algorithm using iteration Newton-Raphson method Finding maximum of a set of given values Calculating factorial of a given number Determining Nth term of Hemachandra numbers (Fibonacci numbers) There are problems where the solution can also be defined as a recursive formula, e.g. f(n) = n! = 1 * 2 * 3 * … *(n-1) * n which can also be expressed as f(n) = n * f(n-1) Lecture 6 Recursion
4
Recursive definitions Recursion is the process of defining something in terms of itself. It is an elegant mathematical concept permits very concise definition of operations nth term f(n) of Hemachandra (Fibonacci) series is given by f(n) = f(n-1) + f(n-2) [nth term is defined in terms the (n-1)th and (n-2)th terms] Lecture 6 Recursion
5
Recursive definitions … For such definitions to be meaningful and valid, some ‘initial conditions must be defined, otherwise the recursive process simply cannot begin. Thus, n! is defined as f(0) = 1 f(n) = n * f(n-1), for n > 0 Similarly, terms in Hemachandra (Fibonacci) series are defined as f(0) = 1 f(1) = 1 f(n) = f(n-1) + f(n-2), for n > 1 Lecture 6 Recursion
6
Execution of the iterative solution To understand what happens internally when recursion is used, we should first look at the iterative solution int factorial (int n){ int f = 1; if (n == 0) return f; for (i =1; i <= n, i++){ f = f * i; }; return f; } Lecture 6 Recursion
7
function invocation Assume that the above function is invoked in the main program in following manner … int n =3, answer; answer = factorial (n); cout << answer; --- What happens when the function call is made ? Lecture 6 Recursion
8
function invocation The control is handed over to factorial function with value of the parameter n = 3 The computer allocates memory to the variables used in the function, i.e., to f, i, n This n is different from the n in main program, hence it gets a separate memory location Lecture 6 Recursion
9
Function invocation Lecture 6 Recursion Set up a logical block for factorial execution Allocate memory to variables n, i, f Calculate answer Release allocated memory and go back Remember the instruction being executed, save context Collect all parameter values Hand over control to function Restore old context and resume execution main function
10
Function invocation There are two type of ‘overheads’ which accompany a function call Additional memory has to be allocated every time a function is invoked For the data elements defined and used in the function For temporarily storing the ‘context’ of the current program Additional computational overhead For transfer of control, parameter transfer, etc. Lecture 6 Recursion
11
Factorial function using recursion #include using namespace std; // prog6-1.c // calculate factorial of a given integer n, int findfactorial(int n){ int f; if (n ==0) { f = 1;} else { f = findfactorial (n-1) * n; } return f; } Lecture 6 Recursion
12
Factorial … recursion int main(){ int n, fact; cout << “give an integer value”; n = 3; fact = findfactorial(n); cout << “factorial is “ << fact return 0; } Lecture 6 Recursion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.