Recursion

Recursion in C
In C, When a function calls a copy of itself then the process is known as Recursion. To put it short, when a function calls itself then this technique is known as Recursion. And the function is known as a recursive function.

You have to be more careful when you are using recursion in your program. You just cannot use recursion in all your problems because it will make your program more complex and difficult.

Recursion can be used in case of similar subtasks like sorting, searching, and traversal problems. While using recursion, you will have to define an exit condition on that function, if not then it will go into an infinite loop.
  • Syntax of recursive function in C:-

void do_recursion()
{
    ... .. ...
    do_recursion();
    ... .. ...
}
int main()
{
    ... .. ...
   do_recursion();
    ... .. ...
}
Working of recursion in C:-
Below is a flowchart of how recursion works:-


flow chart showing recursion in C

The recursion will go in an infinite loop until some condition is met. So, to prevent it from going into an infinite loop, you can make use of the if…else statement or define an exit condition in the recursive function.
Posted on by