Recursion Function
• A function is called ‘recursive’ if a statement
within the body of a function calls the same
function.
• In other word when a function call itself then
that function is called Recursive function.
• Recursive function are very useful to solve many
mathematical problems like to calculate
factorial of a number, generating Fibonacci
series, etc.
Advantage and Disadvantage
• Advantage of Recursion
– Function calling related information will be maintained
by recursion.
– Stack evaluation will be take place by using recursion.
– In fix prefix, post-fix notation will be evaluated by using
recursion.
• Disadvantage of Recursion
– It is a very slow process due to stack overlapping.
– Recursive programs can create stack overflow.
– Recursive functions can create as loops.
Example Factorial of a given Number
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
Int f,num;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
f=fact(num);
printf("Factorial: %d",f);
getch();
}
int fact(int n)
{
if(n<0)
printf("Error! Factorial of a negative
number doesn't exist.");
if(n==0)
return(1);
else
{
return(n*fact(n-1));
}
}
Output
Enter any number: 5
Factorial: 120
Example Fibonacci using Recursive
#include <stdio.h>
#include<conio.h>
int fibonacci(int i);
void main()
{
int i;
clrscr();
for (i = 0; i < 10; i++)
{
printf("%dtn", fibonacci(i));
}
getch();
}
int fibonacci(int i)
{
if(i == 0)
{ return 0; }
if(i == 1)
{ return 1; }
return fibonacci(i-1) +
fibonacci(i-2);
}
Output:
0 1 1 2 3 5 8 13 21 34

3-Recursive Function in programming .pptx

  • 1.
    Recursion Function • Afunction is called ‘recursive’ if a statement within the body of a function calls the same function. • In other word when a function call itself then that function is called Recursive function. • Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc.
  • 2.
    Advantage and Disadvantage •Advantage of Recursion – Function calling related information will be maintained by recursion. – Stack evaluation will be take place by using recursion. – In fix prefix, post-fix notation will be evaluated by using recursion. • Disadvantage of Recursion – It is a very slow process due to stack overlapping. – Recursive programs can create stack overflow. – Recursive functions can create as loops.
  • 3.
    Example Factorial ofa given Number #include<stdio.h> #include<conio.h> int fact(int); void main() { Int f,num; clrscr(); printf("Enter any number: "); scanf("%d",&num); f=fact(num); printf("Factorial: %d",f); getch(); } int fact(int n) { if(n<0) printf("Error! Factorial of a negative number doesn't exist."); if(n==0) return(1); else { return(n*fact(n-1)); } } Output Enter any number: 5 Factorial: 120
  • 5.
    Example Fibonacci usingRecursive #include <stdio.h> #include<conio.h> int fibonacci(int i); void main() { int i; clrscr(); for (i = 0; i < 10; i++) { printf("%dtn", fibonacci(i)); } getch(); } int fibonacci(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fibonacci(i-1) + fibonacci(i-2); } Output: 0 1 1 2 3 5 8 13 21 34