Professor Turing

Study guides

What is recursion in programming?

Last updated: 2026-11-26

Recursion is a way of solving a problem in which a function calls itself on a smaller version of the same problem. It needs a base case, an input it can answer directly, and a recursive case that moves each new call toward that base case.

Start learning No account needed

1. The two parts

Here is a Python function that adds up the digits of a whole number that is zero or more.

def digit_sum(n):
    if n < 10:
        return n
    return n % 10 + digit_sum(n // 10)

The base case is a number below 10: its digit sum is the number itself. In the recursive case, n % 10 is the last digit and n // 10 is the number with that digit removed. The new argument has one digit fewer, so the calls head toward a single digit.

2. A worked example: trace digit_sum(472)

Each call that has not finished waits on the call stack, with its own copy of n and its place in the code. A call cannot add its last digit until the call below it returns.

StepWhat happensCalls still open
1digit_sum(472) needs 2 + digit_sum(47)472
2digit_sum(47) needs 7 + digit_sum(4)472, 47
3digit_sum(4) is the base case and returns 4472, 47, 4
4digit_sum(47) returns 7 + 4 = 11472, 47
5digit_sum(472) returns 2 + 11 = 13472

The calls open from the largest number down and finish in the reverse order. The adding happens on the way back.

3. A common mistake: a base case the calls miss

Having a base case in the code is not enough. The calls have to reach it.

def count_down(n):
    if n == 0:
        return "done"
    return count_down(n - 2)

Starting at 4, the arguments are 4, 2, 0, and the function returns. Starting at 5, they are 5, 3, 1, −1, −3, and so on past zero, since none of them equals 0. Python limits how deep calls can go, and in our run the function ended with a RecursionError. With the test changed to n <= 0, the calls from 4 and from 5 both reach the base case and return. To check your own function, ask how the argument changes on each call and why it has to land in the base case, for each input you allow.

4. The script output

A short script, scripts/what-is-recursion-worked-example.py, adds print lines to the functions above and records the arguments. It prints:

call digit_sum(472)
  call digit_sum(47)
    call digit_sum(4)
    base case: return 4
  return 7 + 4 = 11
return 2 + 11 = 13
count_down(4) arguments: 4, 2, 0
count_down(5) first arguments: 5, 3, 1, -1, -3
count_down(5) ended with RecursionError

5. Test yourself

Close this page. On paper, trace digit_sum(9052): write each call, the most calls open at once, and each return value in order. Then predict what happens if the last line passes n instead of n // 10, and say why. Run the code only after you have written your answers.

For the general method, see how to test yourself.

What the sources found, and their limits

Wikipedia, Recursion (computer science) and Call stack. The first describes recursion as solving a problem through smaller instances of itself, splits a definition into base and recursive cases, and says a step that fails to progress toward a base case can keep going without end. The second describes how a call stack tracks where each active subroutine returns to and can hold its local variables. Sources: Recursion (computer science) and Call stack, cited sections read on 2026-11-25.

Python documentation. For the sys module it says the recursion limit is the maximum depth of the Python interpreter stack and that it prevents unbounded recursion from crashing Python. It also says that RecursionError is raised when the interpreter detects that this depth is exceeded, that // divides and rounds down, and that % gives the remainder. Sources: sys.getrecursionlimit, RecursionError and binary arithmetic operations, cited sections read on 2026-11-25. Limits: the examples are ours and use Python. Other languages handle deep recursion differently, and the functions here assume whole-number input.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.

Bring a recursive function you are trying to trace. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller, so you work it out yourself. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.

FAQ

What is a base case? An input the function answers without calling itself again. The recursive calls need to reach it.

Why do I get a RecursionError in Python? The calls went deeper than Python's recursion limit. Check that each call moves toward a base case. A correct function can also reach the limit on a large enough input.

Start with what you're working on

Tell the tutor which function you are tracing and let it ask you the questions.

Start learning No account needed

See also: how to learn programming without copying code · stack and queue · breadth-first and depth-first search · all study guides