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>Sometimes the most basic information is important. What it appears you may <em>not</em> yet have realised is that Python creates a <strong>new (local) namespace</strong> for each function call. Not only that, but the function you use as an example doesn't even really take much advantage of recursion. But consider this simple function:</p>

<pre><code>def factorial(n):
    if n==1:
        return 1
    else:
        return n * factorial(n-1)
</code></pre>

<p>When you make the call <code>factorial(2)</code> a namespace is created in which the name <code>n</code> is bound to the value 2, and execution of the function code starts. Since <code>n</code> isn't 1 it then has to compute its return value, during which it executes the call <code>factorial(1)</code>, so the interpreter creates a new namespace in which the name <code>n</code> is bound to the value 1, and execution of the function code starts. This time, <code>n</code> <em>is</em> 1, and so it returns the value 1 (after destroying the namespace).</p>

<p>This completes the computation of the first call's multiplication operands, so it now multiplies <code>n</code> (which in this original namespace is 2) by <code>factorial(1)</code> (which it now knows is 1) to get 2, which it then returns (after destroying its namespace).</p>

<p>Does this tediously detailed description help at all? The idea of recursion isn't an easy one to understand.</p>

python recursion