why its printing result in reverse order?
<p>I am trying to understand this program :</p>

<pre><code>def listSum(arr, result):
    if not data:
         return result

    print("print final", listSum(arr[1:], result + arr[0]))
    print("print A:", arr[1:])
    print("print B:", result + arr[0])

listSum([1, 3, 4, 5, 6], 0)
</code></pre>

<p>Its printing result in reverse order :</p>

<pre><code>print final 19
print A: []
print B: 19
print final None
print A: [6]
print B: 13
print final None
print A: [5, 6]
print B: 8
print final None
print A: [4, 5, 6]
print B: 4
print final None
print A: [3, 4, 5, 6]
print B: 1
</code></pre>

<p>Please make me understand what is going on here? If this happening causes of recursion please make me understand how and why ? Explain in depth please</p>

<p>Let's have a more simple example that displays the same behaviour</p>

<pre><code>def rev_printer(n):
    if n:
        print(rev_printer(n-1))
    return n
</code></pre>

<p>Then <code>rev_printer(3)</code> prints</p>

<pre><code>0
1
2
3
</code></pre>

<p>Why?  For each <code>print</code> to finish, it has to do all the work of<code>rev_printer(n-1)</code> and get the return value to print. The first value to be finished is <code>rev_printer(0)</code>, because it doesn't have a print statement in it.  </p>

<p>So the <code>print</code> statement that finished first is the last one to get started, and so the values are printed in reverse order. </p>

python python-2.7 python-3.x