recursion code counting down and counting up My apologies in advance if this is going to cause further confusion.  So, I wrote a very simple recursive method shown below. As you see line # 3 keeps track of value of n before recursive call and line # 5 keeps track of n after recursive call. As you can see both lines # 3 and 5 are printed to see the change in n value. I am puzzled why line 5 is printed in increments of one six times where n value goes from 0 to 5 as the unexpected (to me) output shows: "after: n: 0" ... "after: n: 5".  

1    def countdown(n)
2      return n if n == -1
3      p "before: n: #{n}" 
4      countdown(n-1)
5      p "after: n: #{n}"
6    end
7
8    countdown(5)


Output:

"before: n: 5"
"before: n: 4" 
"before: n: 3"
"before: n: 2"
"before: n: 1"
"before: n: 0"
"after: n: 0"
"after: n: 1"
"after: n: 2"
"after: n: 3"
"after: n: 4"
"after: n: 5"
Look at the definition of your countdown function. 

If I call countdown (5), then it prints "before: n = 5", then it does lots of stuff including lots of printing, then it prints "after: n = 5" and finishes. So your output must start with "before: n = 5" and it must end with "after: n = 5" and all kinds of things might happen in between. 

And that's exactly what happens.