What is recursion in C?
Long Answer
Views 1006
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:
- Winding phase
- 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 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
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of C Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.