1 / 12

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. Two-week ISTE workshop on Effective teaching/learning of computer programming. Overview. Recursion

Télécharger la présentation

Two-week ISTE workshop on Effective teaching/learning of computer programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 6, Recursion Thursday 1 July 2010 Two-week ISTE workshop onEffective teaching/learning of computer programming

  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 Remember the instruction being executed, save context Collect all parameter values Hand over control to function Restore old context and resume execution Set up a logical block for factorial execution Allocate memory to variables n, i, f Calculate answer Release allocated memory and go back main function Lecture 6 Recursion

  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 <iostream> 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

More Related