# Recursion

> A function that calls itself on a smaller job. A stop rule ends the chain.

**Field:** Software Development  
**Category:** Programming Concepts  
**Also known as:** Recursive Function

## Think of it like

Think of it like nesting dolls. Open one, find a smaller one, and go on to the tiny one.

## Definition

Recursion is when a function calls itself. Each call works on a smaller part of the problem. A stopping rule, called the base case, ends the chain when the part is small enough. Then each call hands its answer back up to the one before. Without a base case, the calls never stop and the program crashes.

## Example

A countdown function prints 3, then calls itself with 2, then 1. At 0 the base case prints Go! and stops.

## Example code

```python
def countdown(n):
    if n == 0:
        print("Go!")
        return
    print(n)
    countdown(n - 1)

countdown(3)   # 3, 2, 1, Go!
```

Each call is one smaller, until n reaches 0 and the chain stops.

## Why it matters

Some problems are built from smaller copies of themselves, like folders in folders. Recursion fits those.

## Common mistake

Forgetting the base case, or never getting closer to it. The calls pile up until the program crashes.

## Three principles

- **Calls itself.** The function uses itself on a smaller part.
- **Needs a base case.** A stop rule ends the chain.
- **Answers flow back.** Each call hands its result up.

## Related terms

- [Function](https://learn.benibix.com/term/development/function)
- [Function Call](https://learn.benibix.com/term/development/function-call)
- [Stack Trace](https://learn.benibix.com/term/development/stack-trace)
- [Loop](https://learn.benibix.com/term/development/loop)
- [Base Case](https://learn.benibix.com/term/development/base-case)
- [Backtracking](https://learn.benibix.com/term/development/backtracking)

## Taught in

- [Recursion in Python](https://learn.benibix.com/learn/development-concepts/recursion-in-python): Programming Basics, 14/18

---

Source: https://learn.benibix.com/term/development/recursion
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
