Question: I was doing the first recursion exercise on this here: <http://cscircles.cemc.uwaterloo.ca/16-recursion/>

I followed the obvious hint and made this:

```
def countup(n):
  if n == 0:
    print('Blastoff!')
  else:
    countup(n - 1)
    print(n)

```

So the code basically counts up from 0(blastoff) to n. I just don't understand how it works - i looked in the visualiser and it goes through countup(n) to countup(0) at which point it print's off 'Blastoff', all good. After this it sort of...stays in the else loop and starts printing out the previous n values..why would it do this. Does it store the n values for some reason and even if it does how does this codes mechanism work exactly? Any help will be appreciated. Many thanks.

Answer: 
It uses recursion to print the numbers.  

The best way to understand this is by an example:

```
countup(3)
  countup(2)
    countup(1)
      countup(0)
        n == 0
        print('Blastoff!')
    print (1)
  print(2)
print(3)

```

Each time the function is called, it calles the same function with `n-1`, until `n==0`. At this point, it begins printing numbers as shown.
Consider the variant:

```
def countup(n):
  if n == 0:
    print('Blastoff!')
  else:
    print(n, end=', ')
    countup(n - 1)
       # print(n, end=', ')    reverse  

```

Prints:

```
5, 4, 3, 2, 1, Blastoff!

```

Vs reverse the order of the print with the recursive call:

```
def countup(n):
  if n == 0:
    print('Blastoff!')
  else:
        # print(n, end=', ')    reverse  
    countup(n - 1)
    print(n, end=', ')

```

Prints:

```
Blastoff!
1, 2, 3, 4, 5, 

```

You can now see that with the recursive call of `countup(n - 1)` before the `print(n, end=', ')`, the function must go all the way to the end test of `if n == 0:` before any print is encountered.

Once the condition of `if n == 0:` is met, then the entire call chain is unwound printing the arguments to `countup` in the reverse order. 

Consider as another classic recursive example reversing a string:

```
def rrev(s):
    if s == "":
        return s
    else:
        return rrev(s[1:]) + s[0] 

```

Or, more succinctly:

```
def rrev(s):
    return rrev(s[1:]) + s[0] if s else s

```