Recursion
Recursion
Recursion
When a function calls a copy of itself then the process is known as
Recursion.
When a function calls itself then this technique is known as Recursion. And
the function is known as a recursive function.
Any function which calls itself is called recursive function, and such
function calls are called recursive calls.
Recursion involves several numbers of recursive calls.
Syntax of Recursive function
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
Working of Recursion
Types of recursion in C
There are two types of recursion present in the C programming language.
Direct Recursion
Indirect Recursion
1. Direct Recursion in C
If a function calls itself directly then the function is known as direct
recursive function.
If a function calls itself, it’s known as direct recursion.
Syntax of direct recursion
void recursion()
{
// some code...
recursion();
// some code...
}
Indirect Recursion
If the function f1 calls another function f2 and f2 calls f1 then it
is indirect recursion
It is also called mutual recursion.
Syntax of indirect recursion
void f1();
void f2();
void f1()
{
// some code...
f2();
// some code...
}
void f2()
{
// some code...
f1();
// some code...
}
Example 1
Explanation:-
Example 2
Example 3