Back to computer sciencecomputing use cases

Computer ScienceComputingHigh schoolKey Stages 4 and 5

How to teach recursion

Recursion is a function calling itself, and every recursive function needs a base case that tells it when to stop. This page explains both, with a free activity where students run recursive code in Python or Java and Sparky asks for the base case instead of fixing it.

Title card reading 'How to teach recursion', next to three chat cards: a student says his countdown should stop on its own at 0, Sparky asks which call should be the last one, and he explains why fibonacci keeps doubling its calls.

"My students use Flint to practice coding, receiving instant feedback and tailored support that helps refine their skills at their own pace. This has been particularly effective in addressing individual learning gaps and fostering a growth mindset."

Ali ShameemAli Shameem, Computer Science and Cybersecurity Teacher at Brewster Madrid

Why this matters

A student can write the recursive call correctly and still believe the function will stop by itself when it reaches 0. When the code runs, the numbers fall past 0 into negatives and end in a long red error that never mentions a base case. The student guesses at a fix, and the teacher has to wait for the unit test to find out who still thinks the function knows when to stop.

Recursion and the base case

Recursion is a function calling itself to solve a problem made of smaller versions of the same problem. A recursive function has a base case, which ends the recursion, and a recursive case, which calls the function again (MDN).

Without a base case, the calls never end until the language stops them. Recursion in Python ends in a RecursionError when the calls go past Python's maximum depth (Python documentation). Recursion in Java ends in a StackOverflowError, which Java throws when a program recurses too far and overflows the stack (Oracle). AP Computer Science A, which is taught in Java, lists executing recursive methods in its Data Collections unit (College Board).

In the Flint activity, each student writes and runs four recursive functions in Python or Java, from a countdown to fibonacci, and sends each one to Sparky. Unlike a worksheet, Sparky reads the code the student ran and asks about the base case that student wrote, without writing the fix.

Sparky gets Diego to find his own base case

Diego ends up writing his own base case and explaining why fibonacci(35) is slow. Every student gets the same activity, and Sparky's replies depend on the code and answers each student sends.

Sparky works from Diego's own codeSparky sends Diego back to something he already wrote or ranA right answer gets a harder function
D

Diego

Interest: Rocket launchesStarting point: Thinks the function knows when to stop

Diego

it should stop on its own when n gets to 0 since n keeps getting smaller. but when i ran it, it printed 5 4 3 2 1 0 and then kept going -1 -2 -3 all the way down and then said RecursionError: maximum recursion depth exceeded.

Sparky

Python only does exactly what the code says: print n, then call countdown(n - 1). Which call should be the last one, and what has to be true about n at that point?
An interactive call stack Sparky made in the chat. At the top is Diego's countdown code. Below are three buttons: Next call, Run to crash and Reset. Seven frames are stacked from countdown(5) with n = 5 at the bottom up to countdown(-1) with n = -1 highlighted at the top. Under the stack it reads Stack depth: 7, and: The stack keeps growing. Python stops it forcefully at depth 1000.
InteractiveWhat happens when countdown runs?

Diego

it works now!! 5 4 3 2 1 Liftoff!
Diego's fixed Python code in Flint's code editor: if n equals 0, print Liftoff and return; otherwise print n and call countdown with n minus 1. The Output panel shows 5, 4, 3, 2, 1 and Liftoff!
CodeDiego's countdown with a base case

Understanding check

Now try running fibonacci(35). How long does it take, and why do you think it's so slow?

Diego

fibonacci(35) took a few seconds and printed 9227465. i think its slow because the answer is a huge number

Sparky

The number itself isn't the issue. factorial(35) would give a way bigger number and still be instant.

Diego

every call splits into two more calls so it keeps doubling, and it redoes stuff it already figured out

Wrote his own base case, then explained why fibonacci is slow

Diego is an example student, and Sparky's replies are shortened.

What Flint did for Diego

  • When Diego's countdown ran past 0, Sparky built a call stack from his own code to step through.
  • Sparky asked which call should be the last one instead of writing the base case for him.
  • When Diego blamed fibonacci's huge answer, Sparky pointed him back to his fast factorial to find the real reason.

A teacher can't do this by hand for 30 students, and Sparky does it in every student's chat.

The activity page shows who still thinks the function stops by itself

While the class works, Needs attention shows the teacher which student is stuck and on which message. After three or more students submit, activity analytics lists what the class explained well, where students went wrong, and a follow-up to practice next.

Flint's activity page for the recursion activity. Needs attention shows that one student has run countdown three times with no base case and wrote 'it should stop at 0 on its own'. Activity analytics says most students named the base case before writing sum_list, and 18 students explained the slow fibonacci with repeated calls. 7 students wrote a base case that prints Liftoff but doesn't return. The follow-up suggests tracing countdown(3) one call at a time, then writing a recursive function that counts the vowels in a word. The Sessions table lists three students: two submitted and one still in progress.

Example activity page

After a computer science class writes its first recursive functions

The school and students are made up for this page.

Set up the activity for free

Open the recursion activity in Flint's public library, select Use activity to copy it into a free Flint account, and assign it to a class. Students open the code editor with the Write code button under the message box, pick Python or Java, and press Execute to run their code. Java students end their code with a call such as Main.countdown(5); outside the class, because the editor doesn't run main(). A function that never stops prints more than fits in one message, so students send the code and type the error's last line.

For more computer science activities, see Flint for computer science.

Other use cases

See all use cases →

Learning feels different when it fits you.