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>when print_backward(6) is called then</p>

<pre><code>1 check for 10
2 increase 6 by one num = 7
3 print 7
4 call backward with 7
</code></pre>

<p>for 6 print_backward is not completed yet and print_backward(7) is pushed on the top of stack. The remaining part</p>

<pre><code>print("yeah")
print(7)
</code></pre>

<p>will execute at last when the recursive call is completed. I am skipping check 10 in the below steps. Will include that in last steps.</p>

<pre><code>5 increase 7 by one num = 8
6 print 8
7 call backward with 8 (7 not completed)
8 increase 8 by one num = 9
9 print 9
10 call backward with 9 (8 not completed)
11 increase 9 by one num = 10
12 print 10
return is encountered  
13 call backward with 10 (9 not completed)
14 check for 10 and return. 9 will continue for the execution.
15 in the execution of 9 num is 10 so yeah is printed and then 10 is printed and print_backward for 9 is completed. Similarly for 8 num is 9 which print yeah followed by 9 and so on.
</code></pre>

python recursion