A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Recursion Basics

College Depth 79 in the knowledge graph ☐ I know this ☆ Set as goal
655topics build on this
377prerequisites beneath it
See this on the map →
Call Stack and Function Call ExecutionDefining and Calling Functions+5 more→→Algorithm Design BasicsAnalyzing Recursive Algorithms via Recurrence Relations+13 more
recursion base case call stack self-reference induction

Core Idea

A recursive function is one that calls itself as part of its own definition. Every recursive solution requires a base case (a condition that terminates the recursion) and a recursive case (a call on a smaller or simpler version of the problem). Each function call creates a new stack frame with its own local variables; the call stack grows with each recursive call and shrinks as calls return. Many problems — especially those with naturally recursive structure like trees or mathematical sequences — are elegantly expressed recursively.

How It's Best Learned

Start with factorial and Fibonacci, tracing the call tree by hand. Then implement recursive sum of a list, string reversal, and binary search. Always identify the base case first before writing the recursive case.

Common Misconceptions

Explainer

You already know that functions can call other functions — that is how you have been building programs. Recursion takes this one step further: a function can call *itself*. At first this seems paradoxical, but it is one of the most powerful ideas in programming.

The key insight is that many problems have a self-similar structure. The factorial of 5 (written 5!) equals 5 × 4!. And 4! = 4 × 3!. And so on. Once you see this, a recursive definition writes itself: `factorial(n) = n * factorial(n-1)`. But this chain has to stop somewhere — that is the base case. For factorial, `factorial(0) = 1` by definition. Without the base case, the chain never terminates and you get infinite recursion. Writing the base case *first*, before the recursive case, is the discipline that keeps you out of trouble.

When a recursive function runs, the call stack is doing the real work. Each call to `factorial(n)` creates a new stack frame — a small block of memory holding n and the return address. When `factorial(5)` calls `factorial(4)`, the first frame pauses and waits; when `factorial(4)` calls `factorial(3)`, another frame is added. The stack grows to depth n, then unwinds as each call returns its result up to the caller. This is why variable scope matters: each frame has its own copy of `n`, and there is no confusion between them. It is also why deep recursion can be expensive — you are allocating a frame for every level.

A common bug is calling the recursive function but not returning its result: writing `factorial(n-1)` instead of `return factorial(n-1) * n`. The deeper call runs correctly and computes the right answer, but that answer is thrown away because you never returned it. Always check that your recursive case both calls the function *and* uses the return value.

Recursion is not always the right tool. Simple loops — adding up a list, iterating over a string — are almost always clearer and more memory-efficient as iteration. Where recursion shines is on problems with naturally recursive structure: tree traversals, divide-and-conquer algorithms, parsing nested expressions. If you find yourself writing a loop and keeping an explicit stack to track state, that is a signal that recursion might be the cleaner solution. Over time, you will develop judgment about which to reach for.

Practice Questions 3 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Call Stack and Function Call Execution → Recursion Basics

Longest path: 80 steps · 377 total prerequisite topics

Prerequisites (7)

Leads To (15)