Here below a recursive explanation of what is happening.
When n gets down to n=1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 2, that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = 2.
So we go through the first statement of else again, we get n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 2, that now moves out of the third line of the else statement and returns.
On return, we are brought back to the previous iteration of n, n = 3, that now moves out of the first line of the else statement down to the second, which prints 3, then on the third which is another recursive call that restarts the function with n = n - 1 = 2.
So we go through the first statement of else again, we get n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 2, that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 4, that now moves out of the first line of the else statement down to the second, which prints 4, then on the third which is another recursive call that restarts the function with n = n - 1 = 3.
So we go through the first statement of else again, we get n = n - 1 = 2.
So we go through the first statement of else again, we get n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 2, that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 3, that now moves out of the first line of the else statement down to the second, which prints 3, then on the third which is another recursive call that restarts the function with n = n - 1 = 2.
So we go through the first statement of else again, we get n = n - 1 = 2.
So we go through the first statement of else again, we get n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
On return, we are brought back to the previous iteration of n, n = 2, that now moves out of the first line of the else statement down to the second, which prints 2, then on the third which is another recursive call that restarts the function with n = n - 1 = 1.
When n gets down to n = 1 the if part kicks in, prints 1, then returns.
after that what happens?What happens is what happens when any function returns, control returns to the calling function, except that the calling function is the same as the called function. So control returns to the calling functionfbut nownequals 2. So 2 is printed (as you can see) and thefis called again withnequal 1, and so 1 is printed. Thenfreturns, twice this time and sonnow equals 3 and so 3 is printed etc. etc. Really the trick is that there's nothing special about recursion, it's just regular function calls which work exactly the same as any function call.