understanding recursive function python
<p>I am trying to understand what happens when this recursive function is called. The code is supposed to be a trace </p>

<pre><code>def mysum(lower, upper, margin):
    blanks = ' ' * margin
    print blanks, lower, upper
    if lower &gt; upper:
        print blanks, 0
        return 0
    else:
        result = lower + mysum(lower + 1, upper, margin + 4)
        print blanks, result, lower, margin
        return result

if __name__ == "__main__":
    mysum(1, 4, 0)
</code></pre>

<p>the output reads </p>

<pre><code>1 4
     2 4
         3 4
             4 4
                 5 4
                 0
             4 4 12
         7 3 8
     9 2 4
10 1 0
</code></pre>

<p>I don't understand why the function begins to unwind after it returns 0. Can you help me follow through what happens</p>

<p>When one call of the function returns 0 ("bottoms out"), there might be many other calls to the function on the stack, waiting to proceed. When the recursion bottoms out, control returns to the last-but-one function on the stack. It finishes its work and returns, and control returns to the next earlier function on the stack. This continues until all the calls to <code>mysum</code> have been removed from the stack, in reverse order to the order on which they were put on the stack.</p>

<p>Maybe you understood all that already :) If so, please clarify what you mean by "why the function begins to unwind."</p>

python recursion