Recursive Functions
Recursion is a process by which a function includes itself with a condition for its safe exit. It is best suitable for a recursive problem. A typical example is the factorial problem, the programs without and with recursive functions are shown below.
void main()
{
long factorial ( int num); // Forward declaration of
//a function
int num;
printf("\n Enter a number:");
scanf("5d",&num);
printf(" Factorial of 5d is %ld",num,factorial(num));
}
long factorial(int num) // Non recursive
{
long f=1;
while ( n)
{
f = f * n;
n--;
}
return(f);
}
long factorial(int num) // Recursive
{
if(n == 1)
return (1);
else
return( n* factorial(n-1));
}