You are on page 1of 21

Recursive function

By
Pundreekaksha Sharma
Assistant Professor
CSE

©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
• Recursive functions
– Functions that call themselves
– Can only solve a base case
– Divide a problem up into
• What it can do
• What it cannot do
– What it cannot do resembles original problem
– The function launches a new copy of itself
(recursion step) to solve what it cannot do
– Eventually base case gets solved
• Gets plugged in, works its way up and
solves whole problem

©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!
6
– Then for 2!, 2! = 2 * 1!
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 e d
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 e n c e o f re c ursive c a lls. ( b ) Va lue s re turne d fro m e a c h re c u rsive c a ll.

©LPU CSE101 C Programming


Recursion example (factorial code)
This function
calculates
factorial of
numbers

©LPU CSE101 C Programming


Recursion example (fibonacci)
• What is Fibonacci series: …??
• 0, 1, 1, 2, 3, 5, 8...
– Each number is the sum of the previous two
– Can be solved recursively:
• fib( n ) = fib( n - 1 ) + fib( n – 2 )

– Code for the fibonacci function


long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1)+fibonacci( n – 2 );
}

©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


Recursion example (fibonacci code)
This function
calculates
fibonacci
number of
any given
position

©LPU CSE101 C Programming


Recursion example (fibonacci code)
This function
print first n
term of series

©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)

©LPU CSE101 C Programming


Rules for recursive function
1. In recursion, it is essential to call a function itself
2. 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
3. A recursive function can be invoked by itself or by other
function.
4. To stop recursive function, it is necessary to base recursion
on some condition, and proper termination statement such
as exit() or return
5. The user defined function main() can be invoked recursively.

©LPU CSE101 C Programming


Q1

What will be the output of the following C code?

#include<stdio.h>
int f(int n);
int main()
a) 10
{ b) 80
int n=10; c) 30
printf("%d",f(n));
}
d) Error
int f(int n)
{
if(n>0)
return(n+f(n-2));
}

©LPU CSE101 C Programming


Q1

What will be the output of the following C code?

#include<stdio.h>
int f(int n);
int main()
a) 10
{ b) 80
int n=10; c) 30
printf("%d",f(n));
}
d) Error
int f(int n)
{
if(n>0)
return(n+f(n-2));
}

©LPU CSE101 C Programming


Q2
What will be the output of the following C code?

#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned

©LPU CSE101 C Programming


Q2
What will be the output of the following C code?

#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned

©LPU CSE101 C Programming


Q3
A recursive function is
a) Math function
b) Call itself
c) Library function
d) Call main function

©LPU CSE101 C Programming


Q3
A recursive function is
a) Math function
b) Call itself
c) Library function
d) Call main function

©LPU CSE101 C Programming


Q4
Function
0 if n==0
f(n) = {n+ f(n-1) if n>0
will print
a) Sum of n numbers
b) Factorial of number
c) Value of n
d) Nothing

©LPU CSE101 C Programming


Q4
Function
0 if n==0
f(n) = {n+ f(n-1) if n>0
will print
a) Sum of n numbers
b) Factorial of number
c) Value of n
d) Nothing

©LPU CSE101 C Programming


• Next Lecture

Life and existence of a


variable …??
storage classes

©LPU CSE101 C Programming


cse101@lpu.co.in

You might also like