Saturday, 11 July 2020

Recursion



Recursion



As we know C language is a building block of functions that means anything you are doing is achieved through some kind of function call. So learning about function is very important. Recursion is a concept when a function calls by itself from its function body.







When a function called a stock is maintained by the program execution unit to trace the function current status when control calls back to the calling function. The main disadvantage of a recursive function is it may run infinitely when exit logic not handled. An Example of the recursive function is given below to calculate the factorial of a number.
#include<stdio.h>
int fact(int);
void main()
{
     int i;
     printf("enter number for factorial>> ");
     scanf("%d",&i);
     printf("\n\t%d",fact(i));
}

int fact(int n)
{
     if(n==0)
           return 1;
     else
           return(n*fact(n-1));
}

You can understand the flow logic of factorial number by given flow chart.





No comments:

Post a Comment