Recursive function
Recursive function is a function which calls itself again and again in order to perform repetitive tasks.
When using recursive function we need to be careful of defining the exit condition, else, it will be an infinite loop and the program may crash.
Syntax
void recursion() {
// function calls itself
recursion();
}
int main() {
recursion();
}
Below is an example program to perform factorial of a number using recursive function.
#include<stdio.h>
int factorial(int x); //declaring the function
void main()
{
int a, b;
printf("Enter a number...");
scanf("%d", &a);
b = factorial(a); //calling the function named factorial
printf("%d", b);
}
int factorial(int x) //defining the function
{
int result = 1;
if(x == 1)
{
return 1;
}
else
{
rresult = x*factorial(x-1);
}
return rresult;
}
Output
Enter a number...
5
120