Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Topic: Recursive function

©LPU CSE101 C Programming


Outline
• Recursion
• Examples of recursion
– Finding factorial of a number
– Finding Fibonacci series up to nth term
• Recursion Vs Iteration

©LPU CSE101 C Programming


Recursion
Recursion is a problem-solving
approach, that can ...
-Generate simple solutions to
certain kinds of problems that
would be difficult to solve in
other ways
-Recursion splits a problem:
Into one or more simpler versions
of itself Russian Doll

©LPU CSE101 C Programming


Watch this video…………..

©LPU CSE101 C Programming


Recursion

©LPU CSE101 C Programming


Recursion example (factorial)
• Factorial of a number in mathematics
– 5! = 5 * 4 * 3 * 2 * 1
• Another method we have studied is

Values returned
– For 5!, we write 5! = 5 * 4! 120
– Then for 4!, 4! = 4 * 3!
24
– Then for 3!, 3! = 3 * 2!
– Then for 2!, 2! = 2 * 1! 6
2
– Then for 1!, 1! = 1 * 0!
1
– And if its comes to 0,
– 0!=1
– Solve base case (1! = 0! = 1)

©LPU CSE101 C Programming


Recursion example (factorial)
Fin a l va lue = 120
5! 5!
5! = 5 * 24 = 120 is re turn ed
5 * 4! 5 * 4!
4! = 4 * 6 = 24 is re turne d
4 * 3! 4 * 3!
3! = 3 * 2 = 6 is re tu rn e d
3 * 2! 3 * 2!
2! = 2 * 1 = 2 is re turne d
2 * 1! 2 * 1!
1 re turne d
1 1

( a ) Se q u en c e o f re c ursive c a lls. ( b ) Va lue s re turne d fro m e a c h re cu rsive c a ll.

©LPU CSE101 C Programming


Recursive function for factorial
long int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
long int fact=factorial(n);
printf("Factorial is %ld",fact);
getch();
return 0;
}
long int factorial(int n)
{
if (n >= 1)
return n*factorial(n-1);
else
return 1;
}

©LPU CSE101 C Programming


Recursion example (fibonacci)
• Set of recursive calls to fibonacci() function

f( 3 )

return f( 2 ) + f( 1 )

return f( 1 ) + f( 0 ) return 1

return 1 return 0

©LPU CSE101 C Programming


/* Fibonacci Series c language using recursion */
#include<stdio.h>
int Fibonacci(int);
main()
{
int n,i;
scanf("%d",&n); printf("Fibonacci series \n");
for ( i = 0 ; i < n ; i++ )
{
printf("%d\t", Fibonacci(i)); 5
Fibonacci series 0 1 1 2 3
}
return 0;
}
int Fibonacci(int n)
{ if ( n == 0 ) return 0;
else if ( n == 1 ) return 1;
else return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
©LPU CSE101 C Programming
Fibonacci code: using loops
/* Fibonacci Series c language using loop */
#include<stdio.h>
int main()
{
int n, first=0,second=1,next,i;
printf("Enter the number of terms\t"); scanf("%d",&n);
printf("First %d terms of Fibonacci series are:\t",n);
for(i=0;i<n;i++)
{
if ( i <= 1 )
next = i;
else
{next = first + second;
first = second;
second = next;}
printf("%d\n",next);
}
return 0;
} Enter the number of terms 5
First 5 terms of Fibonacci series are:0 1 1 2 3
©LPU CSE101 C Programming
Recursion vs. Iteration
• Repetition
– Iteration: explicit loop(for, while)
– Recursion: repeated function calls
• Termination
– Iteration: loop condition fails
– Recursion: base case reached
• Both can have infinite loops
• Balance
– Choice between performance (iteration) and good software
engineering (recursion)
• Recursive method often slower than iterative; why?
– Overhead for loop repetition smaller than
– Overhead for call and return

©LPU CSE101 C Programming


Rules for recursive function
1. In recursion, it is essential to call a function itself
2. We must always make sure that the recursion bottoms
out:
 A recursive function must contain at least one non-
recursive branch.
 The recursive calls must eventually lead to a non-recursive
branch
Note:
Only the user defined function can be involved in the recursion. Library
function cannot be involved in recursion because their source code
cannot be viewed.
The user defined function main() can be invoked recursively.

©LPU CSE101 C Programming


• Next Lecture

Life time and existence of a variable …??


storage classes

©LPU CSE101 C Programming


cse101@lpu.co.in

You might also like