C Recursion
Recursion
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
The recursion contains two cases in its program body.
Base case:When you write a recursive method or function it keeps calling itself so, the base case is a specific condition in the function when it is met it terminates the recursion. It is used to make sure that the program will terminate otherwise it goes into an infinite loop.
Recursive case: The part of code inside the recursive function which is executed again and again while calling the recursive function is known as the recursive case.
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 case at which the function doesn't recur is called the base case whereas the instances where the function keeps calling itself to perform a subtask, is called the recursive case.
Types of recursion in C
There are two types of recursion present in the C programming language.
- Direct Recursion
- Indirect Recursion
i)Direct Recursion in C
If a function calls itself directly then the function is known as direct recursive function.
In the above example, fib() function is direct recursive. Because the statements inside the fib() function calls the fib() function directly.
ii)Indirect Recursion in C
A function that does not call itself directly then function is known as an indirect recursive function.