Introduction: How to Write a Simple Countdown Recursive Function in Python

A recursive function is a function that calls itself. Recursive functions are an important concept in the programming world. There are many situations where you might find yourself being able to use a recursive function to solve a problem that would otherwise be very difficult for you to solve. In other cases, recursion might not be the most ideal solution. In this tutorial, I will be showing you how to write a simple countdown Recursive Function in Python. After this you should be ready to write your own recursive functions on your own!

Duration: 2—5 minutes

Supplies

  • Beginner/Intermediate Experience with Python
  • An IDE that allows for Python code
    • I’ll be using IDLE on a Mac (iOS 10)
      • Note: other operating systems will work as well
    • Python 3.0 or higher

Step 1: Open Up IDLE

Once it’s open, you’ll want to create a new file. I’ll be naming mine “Countdown”. After this, above is what your screen should look like.

Step 2: Implement the Function Name and Parameter

I will be calling mine “countdown”, and my parameter will be labeled as ‘n’, which will be a number.

Step 3: Create a Base Case for the Function

Recursive functions are fundamentally used for bigger problems that are easier to solve by breaking them up into smaller problems. In our case, we want to be able to count down from a certain number until we get to 0. Therefore, our base case is when n is equal to 0. We’ll have to have something occur when this condition is met. I will be printing “Blast Off”, but you’re able to print “0” or any other message that you’d like.

Step 4: Add Else Statement

Now that we have our base case, we can move on. At this point in our function, we have established what we want to do if we get to 0. Now, we have to decide what we want to do if this is not the case—i.e. if n is not 0. If n is not 0, we want to go ahead and print out whatever value n currently is, so we add a print statement.

Step 5: Add Recursive Step

Now, we are at the point where we have to add the recursive step to our problem. Up until this point, our function prints out whatever value is held by n, but doesn’t count down. We want the function to do what it just did until it gets to 0. Therefore, we add a statement that calls the countdown function again, but this time, the function is called with a value that’s one less than what was just passed in. Let’s say n = 5; in the next iteration, n will be 4, and then n will be 3, and so on until n is equal to 0.

Note: This function works with positive integers as a parameter.

Step 6: Call the Function

We’re almost to the end. We want to make sure that the function we just made actually works. During this step, you’ll want to add a statement that calls for the function to run. In order to do this, you simply write down the name of the function—in this case, “countdown”—and the number you’d like to countdown from.

Note: Again, this function works with integers that are greater than or equal to 0. If you’d like to use decimals, you’ll have to change your base case or the increment in which the parameter decreases in.

Step 7: Run Program

We are now ready to run our program! Click “File” -> “Save” and then “Run” -> “Run Module”. Above is the output that we should get.

Step 8: Run Several Tests

This step is highly encouraged. A key thing that you’ll want to focus on is program correctness. We not only want our code to work with one example but with many. Try adding multiple different tests at the end. This is what Step 6 was. I will be adding numbers 0, 15, and 100 to show that the program works just the same with any positive integer.

Note: I ended up adding a print statement of hyphens in between the functions calls so that the output was clearer. *click on pictures for a clearer view*

Step 9: You Did It!

Congratulations! You’ve written your first recursive function that counts down from a number. You should now understand the basics about writing a recursive function; think about what other functions would be useful to use recursion in, and see if you can write them so that they execute correctly.

Step 10: Troubleshooting

Did something go wrong? Let’s try to think about why this might be occurring! o

  • Are you using an outdated version of IDLE?
    • Sometimes older versions of IDLE behave differently. Make sure to have the most up to date version of IDLE in order for your program to work correctly!
  • Is indentation correct?
    • Python is a very indentation sensitive language. If off by an indentation, your program won't run the same, so make sure all the indentation aligns to the example code above!
  • Are all the variables the same?
    • Make sure to stick to the same variable within your function or python won't know what to refer to.
  • Typos?
    • Make sure all variables and functions are spelled correctly.