The snippet:
def fun(a):
if a > 30:
return 3
else:
return a + fun(a+3)
print(fun(25)) # prints 56
Now my question is this: The return from the function does not seem assigned specifically to a. So how does the program ever reach the a > 30 branch, and terminate?
In practice, the snippet prints 56. How does the function arrive at this value? One answer seems to be: the return value of fun(a+3)
is indeed assigned to a, resulting in a becoming 53 i.e. (25 + (25 + 3))
on the first pass and 56 i.e. (53 + 3)
on the next pass.
I tried:
def fun(a):
if a > 30:
return 3
else:
return fun(a+3)
print(fun(25))
And got 3. So a + fun(a + 3)
should return 28 to the print command.