I have this simple code using recursion that calculates the exponent. I understand how the recursion works here except for the: if exp <= 0: return 1
. Say I call the function to give me five to the second power. If I have it return 1, it will give me the correct value of 25, but if 2 it returns 50, and 3, 75.
I am having a little trouble seeing how exactly this works within the environment:
def recurPower(base,exp):
if exp <= 0:
return 1
return base*recurPower(base,exp-1)
print str(recurPower(5,2))