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>Sorry i seen <a href="http://stackoverflow.com/a/39815739/5904928">your message</a> too late,I just seen three question about recursion which you asked  , I think you are confuse because you don't know the fundamental of recursion , How recursion works. </p>

<p>For understanding recursion ---> you have to understand how stack works , how stack store function and values of program.</p>

<p>I recommend you <a href="http://www.cryptroix.com/understanding-multiple-recursion/" rel="nofollow">read this article</a> which explain from scratch how stack store values and return values in terms of recursion. If want more info about recursion i suggest read first 100 page of <a href="https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%25_sec_1.2.1" rel="nofollow">SICP book</a> </p>

<p>Its not printing result in reverse order its returning values from the stack. </p>

<p>You have to understand two things :</p>

<ul>
<li>Stack works on Last In first Out principal , So Stack return values
which comes last.</li>
</ul>

<p><a href="https://i.stack.imgur.com/RiGdm.png" rel="nofollow"><img src="https://i.stack.imgur.com/RiGdm.png" alt="enter image description here"></a></p>

<ul>
<li>There are two types of recursion :

<ul>
<li>Head Recursion</li>
<li>Tail Recursion</li>
</ul></li>
</ul>

<p>In Head recursion recursive call first find its base condition then execute rest of code. </p>

<pre><code>def head_recursion(x):
    if x==0:
        return
    else:
        head_recursion(x-1)
        print("Head Recursion",x)

head_recursion(3)
</code></pre>

<p>it will print :</p>

<pre><code>Head Recursion 1
Head Recursion 2
Head Recursion 3
</code></pre>

<p>In tail recursion , Everything execute first after recursion call main function. It means In tail recursion Recursive call is last thing in function.</p>

<pre><code>def head_recursion(x):
    if x==0:
        return
    else:
        print("Head Recursion",x)
        head_recursion(x-1)


head_recursion(3)
</code></pre>

<p>it will print :</p>

<pre><code>Head Recursion 3
Head Recursion 2
Head Recursion 1
</code></pre>

<p>In your code its Head Recursion because after recursive call there is print and other code in function , so recursive call is not last thing in function.</p>

<p>As i said above in Head recursion , Recursive call first find its base condition then execute rest of code below of recursive call , Until it keep executing. So as it found base condition two things happen :</p>

<pre><code>It print rest of code below of recursive function.
It start returning values from stack.
</code></pre>

<p>It will print final value when all the work will be done and recursive call will find its base condition. That's why final value 19 executed at last and returned at first (causes of LIFO). </p>

<p>The Stack When you call a function the arguments to that function plus some other overhead is put on the stack. Some info (such as where to go on return) is also stored there. When you declare a variable inside your function, that variable is also allocated on the stack.</p>

<blockquote>
  <p>Deallocating the stack is pretty simple because you always deallocate
  in the reverse order in which you allocate. Stack stuff is added as
  you enter functions, the corresponding data is removed as you exit
  them. This means that you tend to stay within a small region of the
  stack unless you call lots of functions that call lots of other
  functions (or create a recursive solution).that's why it seems reverse
  to you.</p>
</blockquote>

python python-2.7 python-3.x