Recursion in C
Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a function that calls itself and doesn’t perform any task after function call is known as tail recursion. Let us take an example: Example 1
void recurse() { recurse(); /* Function calls itself */ } int main() { recurse(); /* Sets off the recursion */ return 0; }Example 2
#include <stdio.h> int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 4; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }
Output
Factorial of 4 is 24