What is recursive function?
When a function in C program calls by itself, then that function is called recursive function. This process is called recursion.
C program for recursive function:
Factorial and Fibonacci series will be a good example for recursive function as it calls by itself. Let’s discuss about Factorial program here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> int factorial(unsigned int); int main() { int i = 8; printf("Factorial of the number %d is %d\n", i, factorial(i)); return 0; } int factorial(unsigned int i) { if(i < 2) { return 1; } return i * factorial(i - 1); } |
Output:
Factorial of the number 8 is 40320