I am trying to make a basic power recursive function basicpower
as a part of my university assignment. I tried to set the recursion limit to 100 using sys.setrecursionlimit(100)
. When I make a call to basicpower
with any value between 98 and 100 if shows me a RecursionError: maximum recursion depth exceeded in comparison
error. However, if I make a call to basicpower
with any value between 1 and 97 it works perfectly fine. What causes this overhead in the call stack? Why I can not use the whole number specified by sys.setrecursionlimit
?
Code of basic power function:
import sys
sys.setrecursionlimit(100)
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 0:
return 1 # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
print(basicpower(2,98))