C Language



Admission Enquiry Form

  

Recursive Function in C

What is Recursive function in C?

Recursive function is a function which calls it-self from its own body till the condition is true.

Example of Recursive Function to calculate factorial of a number:

#include<stdio.h>
int res,n;
void main()
{
int fact(int);//declaration of function
printf("Enter number");
scanf("%d",&n);
res=fact(n);//calling of function
printf("\nFactorial is %d",res);
}

                 int fact(int x)//definition of function
{
if(x==0 || x==1)
{
return(1);
}
else
{
res=x*fact(x-1);
}
return(res);
}


Output

Enter number 5

Factorial is 120