Base Case

In short: A base case is the condition in a recursive function that returns a result directly, without making another recursive call. Every recursive function needs one; without it the function keeps calling itself until it overflows the call stack.

The base case tells a recursive method when to stop. Otherwise it would keep calling itself forever!

For example, we could add all numbers 1 to n recursively like this:

public static int sum1ToN(int n) { return n + sum1ToN(n-1); }

If we input 3 as our n, this method will take 3, then add 2, then add 1, then add 0, then add -1, then add -2, etc forever (or until the program crashes).

We want to stop adding when n gets to 1. We'd say that our "base case" is n <= 1, and our code might look like:

public static int sum1ToN(int n) { // base case: if (n <= 1) return 1; return n + sum1ToN(n-1); }

Whenever writing a recursive method, be careful not to forget the base case!

Frequently Asked Questions

What is a base case in recursion?

The simplest input a recursive function can answer directly, like returning 1 when n <= 1 in a recursive sum. It's what stops the chain of recursive calls.

What happens if a recursive function has no base case?

It keeps calling itself until the program runs out of call stack space, causing a stack overflow (in Python, a RecursionError).

Can a recursive function have more than one base case?

Yes. A recursive Fibonacci function, for example, usually has two: fib(0) = 0 and fib(1) = 1.

Last updated: October 21, 2026

. . .