I have the following recursive function below that returns the k to the s power of a certain integer.
However I do not understand how it works. The base class is 1 when s becomes 0.
How is it so that the returned value is actually k^s when 1 is returned? Since 1 is returned I would expect powertoK(k, s) to be 1 all the time.
Are there 2 memory addresses one holding 1 and the other as k^s?
def powertoK(k, s):
if s != 0:
return k* powertoK(k,s-1)
else:
return 1
powertoK(3, 7)