What is recursion in C?

When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

Recursive function comes in two phases:

  1. Winding phase
  2. Unwinding phase

Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.

Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.

Example of recursion


int calculate_fact(int);  
int main()  
{  
 int n=5,f;  
 f=calculate_fact(n); // calling a function  
 printf(“factorial of a number is %d”,f);  
  return 0;  
}  
int calculate_fact(int a)  
{  
  if(a==1)  
  {  
      return 1;  
  }  
  else  
  return a*calculate_fact(a-1); //calling a function recursively.  
   }  

Output:

factorial of a number is 120