Recursion is the process where a function calls itself, and the function which calls itself is know as recursive function.

## Characteristics of a recursive function

- A recursive function is a function which calls itself.
- The speed of a recursive program is slower because of stack overheads.
- A recursive function must have terminating conditions, and recursive expressions.

## Advantages of Recursion

- It requires few variables which make program clean.
- It shorten the complex and nested code.

## Disadvantages of Recursion

- It is hard to debug recursive function.
- It is tough to understand the logic of a recursive function.

**Note :-** Recursive function must have a valid terminating condition otherwise it leads to infinite loop.

To understand how recursion works lets have one of the popular example of recursion. In this example we will calculate the factorial of n numbers. The factorial of n numbers is expressed as a series of repetitive multiplication as shown below:

1 |
Factorial of n = n(n-1)(n-2)……1. |

** Example:**

1 2 |
Factiorial of 5 = 5x4x3x2x1 Equals to 120 |

** Program:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> #include<conio.h> int factorial(int); int factorial (int i) { int f; if(i==1) return 1; else f = i* factorial (i-1); return f; } void main() { int x; clrscr(); printf("Enter any number to calculate factorial :"); scanf("%d",&x); printf("\nFactorial : %d", factorial (x)); getch(); } |

Suppose value of i=5, since i is not equal to 1, the statement:

**f = i* factorial (i-1);**

will be executed with i=5 i.e.

**f = 5* factorial (5-1);**

will be evaluated. As you can see this statement again calls factorial function with value i-1 which will return value:

**4*factorial(4-1);**

This recursive calling process continues until value of i is equal to 1 and when i is equal to 1 it returns 1 and execution of this function stops. We can review the series of recursive call as follow:

**f = 5* factorial (5-1);**

**f = 5*4* factorial (4-1);**

**f = 5*4*3* factorial (3-1);**

**f = 5*4*3*2* factorial (2-1);**

**f = 5*4*3*2*1;**

**f = 120;**