Advantage of Recursion

As we know, a program that can be executed using the recursive function can also be executed using the iterative loop.
let's, take an example to calculate the factorial of a number using iteration and recursion.

advantage-disadvantage-recursion

In the above program, using recursion requires fewer lines of code as compared to iteration.
If you want your code to be short and easy to understand then you can use recursion.
recursion plays a very important role in the tree traversal.

Disadvantage of recursion

The recursion requires more time and space for the execution of the program.

The reason behind this is when you use iteration you require less use of stack activation.

When you use recursion, every time the function is called, and at each call, the variable is initialized and stored in the stack, and again the process of popping the variables and returning the values takes extra time.

Sometimes, StackOverflow takes place when a function is called so many times. Some compiler has a limit of stack activation. If you want to use the stack, you must be aware if the memory is available or not.




Recursion in c