Technology Fundamentals

Recursion

Definition

Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. This is achieved by having a function call itself.

Why It Matters

Recursion provides an elegant way to solve problems that can be broken down into smaller, self-similar sub-problems, such as navigating a tree data structure or calculating a factorial.

Contextual Example

To calculate the factorial of 5 (5!), a recursive function would calculate `5 * factorial(4)`, which in turn calculates `4 * factorial(3)`, and so on, until it reaches the base case of `factorial(1)`, which is 1.

Common Misunderstandings

  • A recursive function must have a "base case"—a condition that stops the recursion—otherwise it will lead to an infinite loop and a "stack overflow" error.
  • Any problem that can be solved with recursion can also be solved with iteration (loops), but recursion often results in simpler, more readable code for certain types of problems.

Related Terms

Last Updated: December 17, 2025