How Recursion Depth works

How Recursion Depth works

Recursion Depth

·

1 min read

So Recursion Depth is the limit of the number of nested calls to a function. So in Python and Javascript Engine the maximal recursion depth is limited to 10^4 and if you exceed the limit it will throw an error =>

RecursionError: maximum recursion depth exceeded in comparison { The reason for this error is to avoid stack overflow.}

blog_recursion_coverpc.png

To encounter this limit issue in Python we have a module to increase the recursion limit by a function known as setrecursionlimit(limit) or we can use tail recursion optimization in which we first calculate and then the recursive call to the function is made so that we don't need the current stack frame and can perform the recursive call to function.

But Python Stackframes can be quite big. Tail recursion is not an efficient technique. Rewriting the algorithm iteratively, if possible, is generally preferred because the standard limit is a little conservative