Types of Recursion in C
This section will discuss the different types of recursion in the C programming language. Recursion is the process in which a function calls itself up to n-number of times. If a program allows the user to call a function inside the same function recursively, the procedure is called a recursive call of the function. Furthermore, a recursive function can call itself directly or indirectly in the same program.
Syntax of the Recursion function
In the above syntax, the main() function calls the recursion function only once. After that, the recursion function calls itself up to the defined condition, and if the user doesn't define the condition, it calls the same function infinite times.
Different types of the recursion
Following are the types of the recursion in C programming language, as follows:
When a function calls itself within the same function repeatedly, it is called the direct recursion.
Structure of the direct recursion
In the above structure of the direct recursion, the outer fun() function recursively calls the inner fun() function, and this type of recursion is called the direct recursion.
Let's write a program to demonstrate the direct recursion in C programming language.
0 1 1 2 3 5 8 13 21 34
When a function is mutually called by another function in a circular manner, the function is called an indirect recursion function.
Structure of the indirect recursion
In this structure, there are four functions, fun1(), fun2(), fun3() and fun4(). When the fun1() function is executed, it calls the fun2() for its execution. And then, the fun2() function starts its execution calls the fun3() function. In this way, each function leads to another function to makes their execution circularly. And this type of approach is called indirect recursion.
Let's write a program to demonstrate the indirect recursion in C programming language.
2 1 4 3 6 5 8 7 10 9
A recursive function is called the tail-recursive if the function makes recursive calling itself, and that recursive call is the last statement executes by the function. After that, there is no function or statement is left to call the recursive function.
Let's write a program to demonstrate the tail recursion in C programming language.
Number is: 7 Number is: 6 Number is: 5 Number is: 4 Number is: 3 Number is: 2 Number is: 1
Non-Tail / Head Recursion
A function is called the non-tail or head recursive if a function makes a recursive call itself, the recursive call will be the first statement in the function. It means there should be no statement or operation is called before the recursive calls. Furthermore, the head recursive does not perform any operation at the time of recursive calling. Instead, all operations are done at the return time.
Let's write a program to demonstrate the Head/Non-Tail recursion in C programming language.
Use of Non-Tail/Head Recursive function 1 2 3 4 5
A function is called the linear recursive if the function makes a single call to itself at each time the function runs and grows linearly in proportion to the size of the problem.
Let's write a program to demonstrate the linear Recursion in C programming language.
The maximum number is: 35
A function is called the tree recursion, in which the function makes more than one call to itself within the recursive function.
Let's write a program to demonstrate the tree recursion in C programming language.
Use of Tree Recursion: The Fibonacci number is: 13
Next TopicConvert Uppercase to Lowercase in C