Write An Essay On Recursion Advantages And Disadvantages

615 Words2 Pages

Draw backs of recursion: Recursion consumes more memory and stack space. Every recursive method call produces a new instance of the method, one with a new set of local variables. The total stack space used depends upon the level of nesting of the recursion process, and the number of local variables and parameters. Recursive version is usually slower.
Recursion may perform redundant computations. In sum, one has to weight the simplicity of the code delivered by recursion against its drawbacks as described above. When a relatively simple iterative solution is possible, it is definitely a better alternative

In recursion factorial we must ensure that factorial is never ever called with a negative N. Recursion method less efficient. Recursive version is shorter, clearer and slower. Recursion offers more elegant solutions. Use recursion for clarity and for a reduction in the time …show more content…

Recursion should be used only in the place where we don’t have any other alternative. Adding to that when we are performing computation for large data using recursion and less memory leads to stack overflow. This means when performing recursion stack gets piled up with program execution stack and increases heap memory consumption. So it is always a best practice to use iteration until and unless we can do that using iteration. When we don’t have any alternatives to choose we use Recursion over iteration.
Suppose we need to pass some data to the recursive process we might want to keep count of the number of nodes visited or set of parameters that determine what to do at each node. Or we have to pass some data to every recursive call. This is waste of time and space. We can use global variables but that’s hardly a preferable solution. If we use an iterative solution we can just have a single set of local variables no need to pass recursively anything. This saves time and

More about Write An Essay On Recursion Advantages And Disadvantages

Open Document