Key Concepts

Review core concepts you need to learn to master this subject

Stack Overflow Error in Recursive Function

def myfunction(n): if n == 0: return n else: return myfunction(n-1) myfunction(1000) #results in stack overflow error

A recursive function that is called with an input that requires too many iterations will cause the call stack to get too large, resulting in a stack overflow error. In these cases, it is more appropriate to use an iterative solution. A recursive solution is only suited for a problem that does not exceed a certain number of recursive calls.

For example, myfunction() below throws a stack overflow error when an input of 1000 is used.

Recursion: Python
Lesson 1 of 2

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo