What is recursion in C?

C Programming Language >   Recursion in c >   Introduction to Recursion  

Long Question

1081


Answer:

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


#include <stdio.h>  
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


This Particular section is dedicated to Question & Answer only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.




Join Our telegram group to ask Questions

Click below button to join our groups.