SESSION – 20
RECURSIVE FUNCTION
Session Outcome:
1. Understanding Recursive Functions
2. Examples Of Recursive Functions
Recursive Functions
• Recursive function is closely related to
mathematical induction
• Recursive function is a function that calls itself
• Every Recursive function will have exit or stop
condition
– other wise the recursive function will keep on
calling itself
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Inherently recursive functions
5!
5*4!
4*3!
3*2!
2*1!
1
5!
5*4!
4*3!
3*2!
2*1!
1
Final value=120
1
2!=2*1=2 returned
3!=3*2=6 returned
4!=4*6=24 returned
5!=5*24=120 returned
Finding Factorial Recursively
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
fact(5)
5*fact(4)
4*fact(3)
3*fact(2)
2*fact(1)1
2
6
24
120
Finding Factorial Recursively
𝑓𝑎𝑐𝑡 𝑛 =
1 𝑖𝑓 𝑛 == 0 𝑜𝑟 𝑛 == 1
𝑛 ∗ 𝑓𝑎𝑐𝑡 𝑛 − 1 𝑖𝑓 𝑛 > 1
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Corresponding Recursive Function
int fact(int n)
{
if(n ==1 || n ==0)
return 1;
else
return n*fact(n-1);
}
Finding Factorial Recursively
Complete program is:
#include<stdio.h>
int fact(int);
main()
{
int n;
scanf("%d",&n);
printf("%d",fact(n));
}
int fact(int n)
{
if(n ==1 || n ==0)
return 1;
else
return n*fact(n-1);
}
Sum of Natural Numbers Recursively
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
𝑠𝑢𝑚 𝑛 =
1 𝑖𝑓 𝑛 == 1
𝑛 + 𝑠𝑢𝑚 𝑛 − 1 𝑖𝑓 𝑛 > 1
Corresponding Recursive Function
int sum(int n)
{
if(n ==1)
return 1;
else
return n + sum(n-1);
}
Finding nth term in Fibonacci Series Recursively
𝑓𝑖𝑏 𝑛 =
0 𝑖𝑓 𝑛 == 0
1 𝑖𝑓 𝑛 == 1
𝑓𝑖𝑏 𝑛 − 1 + 𝑓𝑖𝑏 𝑛 − 2 𝑖𝑓 𝑛 > 1
Corresponding Recursive Function
int fib(int n)
{
if( n ==0)
return 0;
if( n ==1)
return 1;
else
return fib(n-1)*fib(n-2);
}

Recursive functions in C

  • 1.
    SESSION – 20 RECURSIVEFUNCTION Session Outcome: 1. Understanding Recursive Functions 2. Examples Of Recursive Functions
  • 2.
    Recursive Functions • Recursivefunction is closely related to mathematical induction • Recursive function is a function that calls itself • Every Recursive function will have exit or stop condition – other wise the recursive function will keep on calling itself Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in
  • 3.
    Inherently recursive functions 5! 5*4! 4*3! 3*2! 2*1! 1 5! 5*4! 4*3! 3*2! 2*1! 1 Finalvalue=120 1 2!=2*1=2 returned 3!=3*2=6 returned 4!=4*6=24 returned 5!=5*24=120 returned
  • 4.
    Finding Factorial Recursively Wednesday,June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in fact(5) 5*fact(4) 4*fact(3) 3*fact(2) 2*fact(1)1 2 6 24 120
  • 5.
    Finding Factorial Recursively 𝑓𝑎𝑐𝑡𝑛 = 1 𝑖𝑓 𝑛 == 0 𝑜𝑟 𝑛 == 1 𝑛 ∗ 𝑓𝑎𝑐𝑡 𝑛 − 1 𝑖𝑓 𝑛 > 1 Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in Corresponding Recursive Function int fact(int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }
  • 6.
    Finding Factorial Recursively Completeprogram is: #include<stdio.h> int fact(int); main() { int n; scanf("%d",&n); printf("%d",fact(n)); } int fact(int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }
  • 7.
    Sum of NaturalNumbers Recursively Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in 𝑠𝑢𝑚 𝑛 = 1 𝑖𝑓 𝑛 == 1 𝑛 + 𝑠𝑢𝑚 𝑛 − 1 𝑖𝑓 𝑛 > 1 Corresponding Recursive Function int sum(int n) { if(n ==1) return 1; else return n + sum(n-1); }
  • 8.
    Finding nth termin Fibonacci Series Recursively 𝑓𝑖𝑏 𝑛 = 0 𝑖𝑓 𝑛 == 0 1 𝑖𝑓 𝑛 == 1 𝑓𝑖𝑏 𝑛 − 1 + 𝑓𝑖𝑏 𝑛 − 2 𝑖𝑓 𝑛 > 1 Corresponding Recursive Function int fib(int n) { if( n ==0) return 0; if( n ==1) return 1; else return fib(n-1)*fib(n-2); }