Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Function calling itself

Similar presentations


Presentation on theme: "Recursion Function calling itself"— Presentation transcript:

1

2 Recursion Function calling itself

3 Introduction Recursive functions are those function which are call themselves repetitively. The function call themselves repetitively until certain condition is satisfied. The recursive function have following types of statements: A statement or condition to determine if the function is calling itself again. Function call which should have argument Conditional statement(if-else) A return statement.

4 Simple Example main() { int i=2; printf(“%d”,i); main(); }

5 Non-Recursive Function for Factorial

6 Computing Factorial Using Recursion
factorial(0) = 1; //Basis or Base case factorial(n) = n*factorial(n-1); // Inductive Step

7 Computing Factorial factorial(3) factorial(0) = 1;
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3)

8 Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1;
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2)

9 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1))

10 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0)))

11 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1)))

12 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1)

13 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2

14 Computing Factorial factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 = 6

15 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(4)

16 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(3)

17 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(2)

18 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(1)

19 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(0)

20 Trace Recursive factorial
animation Trace Recursive factorial returns 1

21 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(0)

22 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(1)

23 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(2)

24 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(3)

25 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(4)

26 factorial(4) Stack Trace

27 Fibonacci Numbers Finonacci series: … indices: fib(0) = 0; fib(1) = 1; fib(index) = fib(index -1) + fib(index -2); index >=2 fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0) +fib(1) = 1 + fib(1) = = 2


Download ppt "Recursion Function calling itself"

Similar presentations


Ads by Google