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>The recursion is quite easy to understand...in your example the function is calling itself multiple times, but with different values:</p>

<p>F(4)-> F(3) -> F(2) -> F(1) -> F(0) when you reach the last one (printing 4 3 2 1), then the function after printing also 0 it is just "return" so it reaches the last iteration. Going back at the point in the stack were F(1) WAS LEFT!! This is important...it will not execute again whole F(1)...your computer will understand and start where it was left...in your case it means "Do nothing" and it will again "return" --> back to the point in the stack where F(2) was and so on again "Do nothing" and return till F(4) that will also do the same!! If you use an additional IF like below you will be surprised to see also the Happy ending at the END, but of course it is completely normal!! When it came back to F(4) then you can still check and do something else for a special case ("Ending")...of course in all the other steps it will NOT match the if case :-)</p>

<p>Hope this solve your doubts!! If not then please let me know.</p>

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

    if n == 0:   
        return   

    countdown(n - 1)  # the recursive call

    if (n==4):
        print("Happy Ending")

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

python recursion