Skip to main content

A function that calls itself is using recursion.

The idea appears throughout both mathematics and programming. Its benefit is that a function can loop through data until it arrives at a result.

Take care when you write one, because it's easy to end up with a function that never stops, or one that eats up too much memory or processor power. Done right, though, recursion is an efficient and mathematically elegant way to program.

Example

A recursive countdown starting at 5:

Prompt
Write a Python program that defines a recursive function countdown(n) which prints numbers from n down to 1, then prints "Done!". Call the function with 5.
Output: complete Python program

Base Case and Recursive Case

Two parts are required in any recursive function:

  • The base case - The condition that ends the recursion
  • The recursive case - The point where the function calls itself with a changed argument

Take away the base case and the function keeps calling itself until Python stops it with a recursion error.

Nothing matters more here than the base case, so always check that its condition will be reached sooner or later.

Example

Spotting the base case and the recursive case:

Prompt
Write a Python program that defines a recursive function factorial(n) with base case returning 1 for n==0 or n==1, and recursive case returning n * factorial(n-1). Print the factorial of 5.
Output: complete Python program

Fibonacci Sequence

A classic recursion example is the Fibonacci sequence. It begins with 0 and 1, and every later number is what you get by adding the two before it:

0, 1, 1, 2, 3, 5, 8, 13, and so on.

The sequence has no end, and the same addition rule applies all the way along.

With recursion, we can work out any chosen number in the sequence:

Example

Find the number at position 7 in the Fibonacci sequence, counting from 0:

Prompt
Write a Python program that defines a recursive function fibonacci(n) returning n if n<=1, otherwise fibonacci(n-1) + fibonacci(n-2). Print the 7th Fibonacci number.
Output: complete Python program

Recursion with Lists

A list can be processed recursively by dealing with just one element on each call:

Example

Add up all the elements of a list:

Prompt
Write a Python program that defines a recursive function sum_list(numbers) which returns 0 for empty list, otherwise returns numbers[0] + sum_list(numbers[1:]). Test with [1, 2, 3, 4, 5] and print the result.
Output: complete Python program

Example

Find the largest value in a list:

Prompt
Write a Python program that defines a recursive function find_max(numbers) which returns numbers[0] for single-element list, otherwise compares numbers[0] with the max of the rest. Test with [3, 7, 2, 9, 1] and print the result.
Output: complete Python program

Recursion Depth Limit

Recursion in Python can only go so deep. By default, the limit is usually around 1000 recursive calls.

You can raise that limit if you need more depth, but doing so could crash your program.

So raise it with care, and for very deep recursion think about using iteration instead.

Example

Print the current recursion limit:

Prompt
Write a Python program that imports sys and prints the recursion limit using sys.getrecursionlimit().
Output: complete Python program
main.py
import sys
sys.setrecursionlimit(2000)
print(sys.getrecursionlimit())