Recursion | Sololearn: Learn to code for FREE!

+1

Recursion

What disadvantages are in using recursion?

2/6/2020 8:29:10 AM

Игорь Ольхин

11 Answers

New Answer

+5

Difficult to understand and recursion depth quickly outruns the stack memory available. for more information see here https://en.m.wikipedia.org/wiki/Recursion

+3

There is solution for problems with stack memory in Python. From functools import lru-cache and from sys import setrecursionlimit.

+3

It can increase recursion depth but it is language specific. Not all languages provide options to increase the recursion depth from the program itself. For many language you need to specify the option as a compiler options. Tail recursion can improve the performance though. The fact remains recursion is difficult to understand, yet for some problems it is the optimal solution.

+3

If you mess up the base case, you get infinite recursion and a stack overflow.

+2

What about it's understanding, you're right)

+2

Thank you for answer) Recursion is the one thing that was making me problems during my learning.

+1

Reduce wastage of time for writing the code

+1

From my experience as ~ swim ~ said it can be difficult to understand especially if not written properly also the maximum recursion depth (1000 in python by default) is reached very quickly. But there are problems that have a recursive nature embedded in their cores that they can only be solved using recursion or otherwise are very very difficult to impliment using loops. For exemple solving a Hanoi tower in recursion is suprisingly easy compared to loops (and i don't even know if it can be done that way). So, i recommend using loops if possible and use recursion when the solution necessits it.

+1

I tried to find the solution of Tower by using loops, and also didn't find it. Usually I use recursion if there's really necessary.

+1

The major disadvantage is complexity of program..and the stack size

0

Don't forget about the base case - is just very important rule, not disadvantage, I think.. It's like a situation in infinite loops.