confusion about a python recursion function
<p>I got a <code>print_backward</code> function code online, but I am so confusing
about how it works and its output. </p>

<p>The below is my code.</p>

<pre><code>def print_backward(num):
    if num == 10:
        return 
    num += 1
    print(num)
    print_backward(num)
    print("yeah")   
    print(num)
    print()

print_backward(6)
</code></pre>

<p>The below is output.</p>

<pre><code>7
8
9
10
yeah
10

yeah
9

yeah
8

yeah
7
</code></pre>

<p>I can understand how it prints from 7 to 10, since each time it call
recursively, num += 1.</p>

<p>But I am confusing, once num achieves 10, the <code>print_backward</code> should
return, then done. It should not print yeah 10, yeah 9, yeah 8, yeah
7. Why this code has called return, how it still can print? How this code works to print backward, which means why I called <code>print(num)</code>, it
can print from 10 to 7? </p>

<p>The function calling happens through stack. </p>

<p>You call print_backward on 6. So stack will have 6. <br>
Now you call it on 7 ---> So stack has 6,7 where 7 is top. <br>
Now you call it on 8 ----> So stack is 6,7,8 <br>
Now on 9, So stack is 6,7,8,9 <br>
Now on 10, So stack is 6,7,8,9,10. <br></p>

<p>Now when function returns , it pops from stack.  <br>
So, you were in function with parameter 10. So, you called return and it gets popped from stack. Now stack is 6,7,8,9. <br>
Now it prints "yeah" and 10 (because num is 9 and we did num = num + 1 before). 
Now it returns and pops 9. Now stack is 6,7,8 <br>
Now it prints yeah and 9 because num is 8 and we did num = num +1 before).</p>

<p>Similarly, it prints other as well.</p>

python recursion