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>In brief, you always make a recursive call before you reach a <code>return</code> statement, until you reach the base case. Then, you always reach a <code>return</code> statement before you reach another recursive call (trivially so, since there is only one recursive call).</p>

python recursion