Recursion in C means executing or repeating a similar process number of times. Recursion in c is the most important concept in c programming.

What is Recursion in C?

Definition for Recursion: Recursion in c is a programming technique in which the function repeated calls itself.
Recursion is a method of programming in which, a function calls itself.
A function calls another function which eventually calls the first function to be called.
See the below image which shows two recursion scenarios,
1.fun1 calls itself.
2.fun1 calls fun2 which eventually calls the fun1.

Recursion in C
Recursion in C

Why do we use Recursion in C?

Normally whenever we write any program if we want to execute a particular function multiple time then we use iteration (looping in c)
when we write a loop and call any function in loop then that function will get executed the number of times the loop executes.
So, instead of using the looping concept for repeated function call we can use recursion concept in c.
The recursion calls function itself infinite times.
To stop infinite call to the same function we can use conditional statement in c.

How does recursion work?

To understand how recursion works lets see one example :
void fun()
{
…….
fun();
……..
}
int main()
{
……
fun();
……
}

How recursion works?
How recursion works?

now we know what is mean by recursion and how it works let us see one program m to implement recursion.

Recursion program in C

#include <stdio.h>
void display(int i)
{
//condition to stop recursive call
if(i<=5)
{
printf("\nRecursive call :%d ",i);
//Recursive call to display function with i valuee
display(i+1);
}
}
int main()
{
display(1);
}

Output:-

Recursion in C

Program description:-
1. First, we write the header file stdio.h which includes in the program.
2. Next, we write one function to implement recursion. As we know in c first we have to write the user-defined function definition before main function. void display(int i) { }this display function accepts one p[parameter as int type which we used as the number of times the recursion function execution.3.
3. Next, we have written the condition if(i<=5) which used to terminate the recursive call to function after condition gets fails.
4. In the condition we printed a number of time recursion executed using the value of i. and called the display() function again (recursive call) with value increased by one.
After writing the user defined function we write them main function. in main we call the function display with an integer value ( the number of times you want to call recursion function).

Write A Comment