Learn one of the most powerful programming languages in the world and become a rockstar developer.
In this tutorial, you will learn to write recursive functions in C programming with the help of an example.
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); }
The following example calculates the factorial of a given number using a recursive function −
#include <stdio.h> unsigned long long int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 12; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }