Recursion Function
When function calls itself (inside function body) again and again then it is called as recursive function. In recursion calling function and called function are same. It is powerful technique of writing complicated algorithm in easiest way. According to recursion problem is defined in term of itself. Here statement with in body of the function calls the same function and same times it is called as circular definition. In other words recursion is the process of defining something in form of itself.
Syntax:
main ()
{
rec(); /*function call*/
rec();
rec();
Example: /*calculate factorial of a no.using recursion*/
int fact(int);
void main()
{
int num;
printf(“enter a number”);
scanf(“%d”,&num);
f=fact(num);
printf(“factorial is =%d\n”f);
}
fact (int num)
{
If (num==0||num==1)
return 1;
else
return(num*fact(num-1));
}
Output:
enter a number
3
Factorial is 6