0

I'm currently working in python.

when defining a recursive function

def f(n):
    if n < 1: return 0
    return f(n-1)

this works perfectly fine with f(800), but gives RecursionError with f(1000)

and I know that RecursionError happens when recursion goes to depth 995 (or 996, 994, not really sure).

my question is why does RecursionError exist in the first place

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Superior
  • 787
  • 3
  • 17
  • 1
    Because every time you recurse python opens up another call stack for the new function call, reserves space allocates stuff etc. This will eventually lead to no more memory present and crash. So there is a soft predefined border (that can be increased, see dupe) that stops your program if you enter too deep into the rabbit hole. Also your function is bogus - simply return 0 and be done. – Patrick Artner Jun 22 '18 at 20:10
  • As my professor said: today we will learn how to use recursion, tomorrow we will learn to use it only when there is no other way and next week will prove that there is alway an other way. – Klaus D. Jun 22 '18 at 20:26

0 Answers0