How two recursion function in program works?
<p>I am learning Recursion and understand single recursion like Factorial . I am facing problem to understand how two recursive function works ? I am trying to understand this code :</p>

<pre><code>def countdown(n,m):

    if n == 0:  # this is the base case
        return  # return, instead of making more recursive calls
    print("before recursion",n)

    countdown(n - 1,m)                    # first recursive call
    print("first recursion",n)       
    countdown(n - 1,m)
    print("Second recursion",n)            # second recursive call



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

<p>And the output is :</p>

<pre><code>before recursion 3
before recursion 2
before recursion 1
first recursion 1
Second recursion 1
first recursion 2
before recursion 1
first recursion 1
Second recursion 1
Second recursion 2
first recursion 3
before recursion 2
before recursion 1
first recursion 1
Second recursion 1
first recursion 2
before recursion 1
first recursion 1
Second recursion 1
Second recursion 2
Second recursion 3
</code></pre>

<p>I tried to understand with visual python :</p>

<p><a href="http://pythontutor.com/visualize.html#code=def%20countdown(n,m%29%3A%0A%0A%20%20%20%20if%20n%20%3D%3D%200%3A%20%20%23%20this%20is%20the%20base%20case%0A%20%20%20%20%20%20%20%20return%20%20%23%20return,%20instead%20of%20making%20more%20recursive%20calls%0A%20%20%20%20print(%22before%20recursion%22,n%29%0A%0A%20%20%20%20countdown(n%20-%201,m%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20first%20recursive%20call%0A%20%20%20%20print(%22first%20recursion%22,n%29%20%20%20%20%20%20%20%0A%20%20%20%20countdown(n%20-%201,m%29%0A%20%20%20%20print(%22Second%20recursion%22,n%29%20%20%20%20%20%20%20%20%20%20%20%20%23%20second%20recursive%20call%0A%0A%0A%0Acountdown(3,4%29&amp;cumulative=false&amp;curInstr=0&amp;heapPrimitives=false&amp;mode=display&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false" rel="nofollow">Visualize Python code live</a></p>

<p>I am understanding first recursion process but when second recursion start , I am facing problem to understand behaviour of second recursion </p>

<p>Let me explain what i am not understanding :</p>

<p>Ok so this is situation where first recursion finish after finding this base condition (n==0)</p>

<p><a href="https://i.stack.imgur.com/9R6aV.png" rel="nofollow"><img src="https://i.stack.imgur.com/9R6aV.png" alt="First recursion found base condition"></a></p>

<p>Now here second recursion start work</p>

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

<p>, My confusion is until i have learned "below part of recursion would not execute until recursion found its base condition so first recursion didn't print <code>"first recursion 1"</code> until it found base condition in this program , i understand this point clearly. But now when second recursion start working it print immediately <code>"Second recursion 1"</code>(<code>print("Second recursion",n)</code> ) without completing recursion ?? My main confusion is this.</p>

<p>If you are confuse please see step 36 to 43 here <a href="http://pythontutor.com/visualize.html#code=def%20countdown(n,m%29%3A%0A%0A%20%20%20%20if%20n%20%3D%3D%200%3A%20%20%23%20this%20is%20the%20base%20case%0A%20%20%20%20%20%20%20%20return%20%20%23%20return,%20instead%20of%20making%20more%20recursive%20calls%0A%20%20%20%20print(%22before%20recursion%22,n%29%0A%0A%20%20%20%20countdown(n%20-%201,m%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20first%20recursive%20call%0A%20%20%20%20print(%22first%20recursion%22,n%29%20%20%20%20%20%20%20%0A%20%20%20%20countdown(n%20-%201,m%29%0A%20%20%20%20print(%22Second%20recursion%22,n%29%20%20%20%20%20%20%20%20%20%20%20%20%23%20second%20recursive%20call%0A%0A%0A%0Acountdown(3,4%29&amp;cumulative=false&amp;curInstr=0&amp;heapPrimitives=false&amp;mode=display&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false" rel="nofollow">Visualize Python code live</a></p>

<p>If i understand this then i would be able to understand tower of hanoi problem. But i want to understand first how second recursion work in program.</p>

<p>Again and again I would suggest not to use any visual tool, just have a look at the code itself! First of all the m value for what is needed in this example??? </p>

<p>Second and most important...as explained in the other post you have to think about a python STACK and going into iteration.
<a href="http://stackoverflow.com/questions/39994167/what-happen-after-recursion-found-base-condition/39994500?noredirect=1#comment67290669_39994500http://">http://stackoverflow.com/questions/39994167/what-happen-after-recursion-found-base-condition/39994500?noredirect=1#comment67290669_39994500</a> </p>

<p>In your program what you are doing is:
1) going down using --> countdown(n - 1,m) # first recursive call 
before recursion 3
before recursion 2
before recursion 1</p>

<p>2) When you n = 1 you will call last time countdown(0,m) (I hope this is fine for you that n-1 = 0 now) and thus the function will just return. Then you will come back in the stack with n = 1 </p>

<pre><code>print("first recursion",n)       
countdown(n - 1,m)
print("Second recursion",n)            # second recursive call
</code></pre>

<p>Now you will print "first recursion 1" as showed from your output, and then going to the countdown again with countdown(0,m) that imply to return again. Thus you will go again back in your stack with n = 1 and print: "Second recursion 1".</p>

<p>Your function for n = 1 is again finished. Then you have to go to your stack and check that you had n = 2 and you were still at</p>

<pre><code> countdown(n - 1,m) # first recursive call
</code></pre>

<p>Hope this is clear for you. Then you follow to the next step and print "first recursion 2" and you go again into countdown(1,m) again n = 2 ==> n-1 = 1.
This will print "before recursion 1" and go ahead to countdown(0,m) that will return and you will go back at that point. </p>

<p>Continue this for all the steps and you will get the output you got!!
Last but not least I don't like to be downgrade, if you are not trying to follow each step :-) so this is my last explanation. Have a nice day!</p>

python python-3.x recursion