iterate(end, foo=0) recurses while end > 0, adding end into foo each call. print(iterate(2)) evaluates as: iterate(2,0) -> foo=iterate(1, 0+2=2) -> foo=iterate(0, 2+1=3); at end=0 the if is skipped and 3 is returned back up the chain unchanged. So the accumulator sums 2 + 1 = 3, and the output is 3. It is effectively summing 2 down to 1.
Community Discussion