def counter(n):
if n < 0:
return
else:
print '1st print', n
counter(n-1)
print '2nd print', n
print counter(3)
# 1st print 3
# 1st print 2
# 1st print 1
# 1st print 0
# 2nd print 0
# 2nd print 1
# 2nd print 2
# 2nd print 3
# None
So after fiddling around with the recursion functions a bit I realized something quite peculiar and I can't quite wrap my head around it. I understand the first part of the function where it prints from 3 to 0, but I don't understand the second part when it prints from 0 to 3 again. Wouldn't the function just stop when n = 0?
ntwice - once before the recursion and once after it. Once all of the recursive calls are done, all the methods that are waiting on their recursive calls printnagain (in the opposite order). Try running it yourself with a pen and paper.