Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.

Similar presentations


Presentation on theme: "Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure."— Presentation transcript:

1 Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure

2

3 Introduction A repetitive algorithm is simply code that needs to be run over and over again. In general, there are 2 approaches to writing repetitive algorithms:- Iteration Recursion

4 Example : Let’s consider a function to calculate factorial of a number. For example, let’s determine the factorial of 5: Factorial (5) = (5*4*3*2*1) = 120

5 Iterative Definition Solve the problem in an Iterative definition. int factorial(int n) { int fact = 1; for( I = 1; I <= n ; ++I) fact *= I; return fact; }

6 RECURSION recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. recursive programming: Writing methods that call themselves to solve problems recursively. 6

7 RECURSION 7

8 8 Recursion

9 9

10 Recursive Functions A function that calls itself 2 parts of a recursive function base case – to stop the function General case - recursive step A recursive function without a base case will cause a function to call itself infinitely until all the computer memory has been consumed. This will either crash the system or cause the program to hang.

11 Algorithm Algorithm factorial(val n ) // Calculates factorial of a number if ( n equal to 1 ) return 1 else return n * factorial( n – 1) end if End recursive BASE CASE GENERAL CASE

12 Designing a Recursive Function The rules for design are the following :- First, determine the base case Then, determine the general case Finally, combine the base and the general case into an algorithm. When combining the 2 parts of the algorithm, we must make sure that we either reduce the size of the problem ( move closer to the base case)

13 Recursive – Identify Base and General case int fact(int n) { if (n < = 1) return 1; else return n*fact(n-1); }

14 Recursive – Identify Base and General case int guess( int A, int B) { return 1 + guess(A-1,B); }

15 Recursion – Tracing 15 int sum(int n)

16 Recursion – Tracing 16 int f(int k, int n)

17 17 Why not to use recursion 1. It is usually slower due to the overhead of maintaining the stack. 2. It usually uses more memory for the stack. Why to use recursion 1. Recursion adds clarity and (sometimes) reduces the time needed to write and debug code (but doesn't necessarily reduce space requirements or speed of execution). Recursion

18 18 Recursion - Tutorial int result (int n)

19 19 Recursion - Tutorial int mystery(int n, int a, int d)

20 Try this! What value will be returned by the function below if A has the value 3 and B has the value 5? int guess( int A, int B) { if(A == 0) return B; else return 1 + guess(A-1,B); }

21 21 Recursion in REAL LIFE


Download ppt "Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure."

Similar presentations


Ads by Google