Recursion
* Recursion is defined as defining anything in terms of itself.
* In recursion, a function calls itself.
* Recursion is one of the most powerful tools in the programming language but is one of the most threatening topics.
Types of recursion
Recursion is of two types depending on whether a function calls itself from within itself, or whether two functions call one another mutually.
(i). Direct recursion
(ii). Indirect recursion
Direct recursion:
When a function calls itself from within itself it is called direct recursion.
Indirect recursion:
When two functions call one another mutually.
Example:1
When a function “A” calls function “B” and function “B” calls function “A”, then it is called indirect recursion.
Example:2
When a function “A” calls function “B” and function “B” calls function “C”, and further function “C” calls function “A” then also it is called indirect recursion.
Difference between direct recursion and indirect recursion
direct recursion | indirect recursion |
When a function calls itself from within itself it is called direct recursion. | When two functions call one another mutually. |
int abc() { ——-; ——–; abc(); } |
int abc() { —-; —–; xyz(); } int xyz() { —-; —; abc(); } |