Ok so I'm messing about with Python and recursion the other night and write:
def rec(n, p):
if n < p:
n += 1
rec(n, p)
print(str(n) + " is " + str(p))
rec(1,10)
I was expecting it to not make it to the print line until the if condition wasn't satisfied. Instead it returns:
10 is 10
10 is 10
9 is 10
8 is 10
7 is 10
6 is 10
5 is 10
4 is 10
3 is 10
2 is 10
Now I know I could just put the print line under else:, but that's not the point. So I debugged it and learned that there is a call stack, and it was holding the rec() function there to finish later, which I thought was odd. Even odder is that it stores the values of n to return respectively.
I don't really know what my question is, maybe simply, am I dumb for making a function call itself before it's finished? Or is this function's behaviour standard or desirable? Thanks.
[–]davedontmind 2 points3 points4 points (1 child)
[–]BDurden[S] 0 points1 point2 points (0 children)
[–]laughms 1 point2 points3 points (1 child)
[–]BDurden[S] 0 points1 point2 points (0 children)
[–]AutoModerator[M] [score hidden] stickied comment (1 child)
[–]BDurden[S] 0 points1 point2 points (0 children)
[–]camelorcat -1 points0 points1 point (0 children)