Basics

What is recursion — a function that calls itself (and where its floor is)

Illustration: nesting dolls one inside another, and the last one solid — the one that doesn't open

If you've ever seen a red Maximum call stack size exceeded or RecursionError in your console, you've already met recursion. Just the kind with no floor. Here's the funny part: recursion isn't about clever code, it's about tasks that contain themselves. Folders inside folders. Replies to replies. Five minutes and it's yours.

What recursion means in plain words

Recursion is when a function calls itself to solve a smaller piece of the same task.

Think of nesting dolls. To "open a doll" you open it and do the exact same thing to the one inside. And so on — until you reach the smallest, solid one that doesn't open. That last one is the floor.

A live example: you need to count all the files in a folder. Inside it are other folders, and inside those, more. You don't know in advance how deep it goes. Writing that with loops hurts; with recursion the task fits in one sentence: "the files in a folder are the files right here plus the files in every nested folder, counted the same way."

Two mandatory pieces: the floor and a step toward it

Every working recursion has exactly two parts. Miss one and the program crashes.

  1. The base case — the situation where the function answers immediately, calling nobody. That's the floor. For dolls, the smallest one. For folders, "there are no nested folders left".
  2. A step toward the floor — each next call must work on a task that is strictly smaller than the last. Otherwise you call the same thing forever.

Here's the ten-line classic — the sum of numbers from 1 to n:

function sum(n) {
  if (n === 0) return 0;   // base case: the floor
  return n + sum(n - 1);   // step down: the task got smaller
}
sum(4); // 4 + 3 + 2 + 1 + 0 = 10

Delete the first line and the function marches into negatives forever. Keep it but write sum(n) instead of sum(n - 1) and it's the same story: the task never shrinks, the floor is never reached.

Where the stack error comes from

Every unfinished function call takes a slot on the call stack — the same pile you see in a stack trace when things break. While sum(4) waits for an answer from sum(3), it sits on the pile. The pile isn't infinite: Python defaults to about a thousand levels, browser JavaScript usually to a few thousand depending on the engine.

Hit the ceiling and you get RecursionError or Maximum call stack size exceeded. In practice it almost always means one thing: there's no floor, or you never reach it. Don't raise the limit — fix the exit condition.

Where you'll actually meet recursion

Not in factorial puzzles, but in data shaped like a tree:

  • Files and folders. Walk a whole project, find every image.
  • JSON of any depth. An object inside an object inside an array — one function walks it all.
  • Comment threads. Replies to replies, any number of levels deep.
  • An HTML page. Element inside element — same nesting dolls, which is why page traversal is almost always recursive.
  • Menus and categories. Section, subsection, sub-subsection.

The tell is simple: if a thing contains things of its own kind, recursion is probably the easier fit.

When recursion is a bad idea

Honestly: more often than beginners think.

  • Plain iteration over a list — use a normal loop. Faster, clearer, no stack risk.
  • Very deep nesting — thousands of levels won't fit the stack; that gets written as a loop with your own "still to visit" list.
  • You find it hard to read — no shame in that. Recursive code is shorter but harder to hold in your head.

One extra ask: if AI wrote you a recursion, find the base case with your eyes first. It's the single most common spot where generated code looks elegant and doesn't work.

Is recursion slower than a loop?

Usually a little, yes: each call means work with memory and the stack. Walking a tree, the difference is invisible; over a million simple steps, it shows. Choose by the shape of the task, not by speed: tree means recursion, list means loop.

What do I do about a stack overflow error?

Three checks in order: (1) is there an exit condition at all; (2) does the argument really shrink on every call; (3) is there an accidental loop — function A calls B, and B calls A back. The third is rarer but takes the longest to find.

Does a beginner need to write recursion?

Writing it — not urgent. Recognising it — yes. You'll meet it in other people's code and in generated code, and knowing "this function calls itself, and here's its floor" saves hours of debugging.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →