What happen after recursion found base condition?
<p>I am trying to understand recursion , I am following stackoverflow answer <a href="http://stackoverflow.com/questions/717725/understanding-recursion">Understanding recursion</a>   but i am not getting what i am looking. until i have learned this :  <code>" if recursion is in program then below block of code will not execute until recursion found its base condition, once it found its base condition then that recursion will never execute again and now below block of code will execute without recursion "</code> is am getting right ?</p>

<p>Here is a program :</p>

<pre><code>def countdown(n):
    print(n)

    if n == 0:   
        return   

    countdown(n - 1)  # the recursive call

countdown(4)
</code></pre>

<p>I tried to take help of python visualized code , so my confusion is first recursive function will call function again and again until it found base condition ,ok i understand this part ,like in this program it found base condtion if <code>n==0</code> so now n is 0 and return value is 0 , i understand this :</p>

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

<p>but what happen after ? why  it return result in reverse order after found base condition ? this is my confusion , why its going reverse ? its like first its unpacking values then its packing old values ? from where n=1 came ?? how ??</p>

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

<p>now n=2 how ? how its going reverse ???? whats happening here ?</p>

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

<p>now n=3 then n=4 and then at last it goes to first where it started , whats happening here , my big confusion is what happen after base condition found </p>

<p>like in 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>when it found base condition that if list is empty return result so after it found base condition why its printing result it reverse ? </p>

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

<p>What you have is simply a <em>call stack</em>. Whenever a function calls another function, that new call is pushed onto the call stack.</p>

<pre><code>def a():
    b()

def b():
    c()

def c():
    pass

a()
</code></pre>

<p><code>a</code> calls <code>b</code>, <code>b</code> calls <code>c</code>. You now have a call stack of <code>a</code> â <code>b</code> â <code>c</code>. Once <code>c</code> returns, the stack will be unwound in the reverse order: <code>c</code> will get discarded from the top, then <code>b</code>, finally <code>a</code>.</p>

<p>This is more apparent when there's something <em>after</em> the function call:</p>

<pre><code>def a():
    b()
    print('foo')

def b():
    pass

a()
</code></pre>

<p><code>a</code> will get executed and call <code>b</code>. Now you have a call stack of <code>a</code> â <code>b</code>. <code>b</code> finishes, popping off the stack. You now have a call stack of just <code>a</code>. Then <code>'foo'</code> will be output. Then the stack unwinds completely.</p>

<p>This is no different with a recursive function, only that instead of three different functions, you're calling the same function. You'll have a call stack of <code>countdown</code> â <code>countdown</code> â <code>countdown</code> â â¦, or <code>countdown(4)</code> â <code>countdown(3)</code> â <code>countdown(2)</code> â â¦</p>

<p>BTW, the call stack is what you see as <em>stack trace</em> whenever you see Python's default error output:</p>

<pre><code>Traceback (most recent call last):
  File â¦, line â¦, in &lt;module&gt;
  File â¦, line â¦, in a
  File â¦, line â¦, in b
  File â¦, line â¦, in c
SomeError: some message
</code></pre>

<p>It tells you what functions where called in what order to get to the place the error occurred.</p>

python recursion