VECTRABETAMore learning problems
Programming

Recursion — why you can trace it down but not back up

“I'm confused with the last line especially because if n = 5 for example, then fibonacci(4) + fibonacci(3) would be called and so on but I don't understand how this algorithm calculates the value at index 5 by this method.”

asked on Stack Exchange · 568,951 views, as of 2026-10-20

The short answer

Following an example lets you watch the steps someone else chose. Solving one yourself means choosing them — and with recursion the two choices that matter are when the function stops (the base case) and what each paused call does with the answer when it comes back. Most teaching shows the first and skips the second.

Most explanations of recursion focus on the function calling itself. That part is easy to say and easy to nod along to.

The part that actually breaks people is what happens after the base case — when all those paused calls have to unwind, in order, each one finishing the piece of work it left half-done.

If you can trace one call going down but lose the thread on the way back up, you haven't finished learning recursion. You've learned to write it, not to predict it.

The call stack of f(4), from the first call to the final answerA timeline of f(4) in ten rows. Calls go down one row at a time: f(4) pauses, then f(3), then f(2), then the base cases f(1) and f(0) return 1 and 0 without further calls. The paused frames then finish in reverse order: f(2) returns 1, the paused f(1) inside f(3) returns 1, f(3) returns 2, f(4)'s second call f(2) returns 1, and f(4) finally returns 3.order of eventsf(4)pauses — owes f(3) + f(2)CALLf(3)pauses — owes f(2) + f(1)CALLf(2)pauses — owes f(1) + f(0)CALLf(1)base case — returns 1BASEf(0)base case — returns 0BASEf(2)= 1 + 0 — returns 1 upRETURNf(1)paused in f(3) — returns 1RETURNf(3)= 1 + 1 — returns 2 upRETURNf(2)second call for f(4) — returns 1RETURNf(4)= 2 + 1 — returns 3DONEa call is made — the frame pauses, waitinga value returns — the frame finishes and is removed
Each row is one call frame, in the order events happen. Calls go down — frames pause, owing work. Values come back up to whoever is waiting. f(4) can only answer after every row below it has answered.

Worked example — f(4), step by step

  1. 1. f(4) needs f(3) + f(2), so it pauses.

    The addition cannot happen until both calls answer — the frame sits on the stack remembering it still owes '+ f(2)'.

  2. 2. f(3) needs f(2) + f(1), so it pauses too.

    Now two frames are waiting, and nothing has been calculated yet.

  3. 3. f(2) needs f(1) + f(0), so it pauses as well.

    Three frames deep, and the falling has not stopped on its own — only the base case can stop it.

  4. 4. f(1) hits 'if (n <= 1) return n' and returns 1 immediately.

    This is the base case: the one place where no new call is made, so the stack finally stops growing.

  5. 5. f(0) hits the same base case and returns 0.

  6. 6. The inner f(2) receives 1 and 0, adds them, returns 1 — and its frame is destroyed.

    This is the unwind: the paused work finally runs, in the exact reverse order of the calls.

  7. 7. The f(1) paused inside f(3) returns 1.

    It was already waiting and needed nothing else.

  8. 8. f(3) now has both answers: 1 + 1 = 2, and returns to f(4).

  9. 9. f(4) receives 2, its second call f(2) answers 1, and f(4) returns 3.

    The frame that started everything is the last one to finish.

Nine frames were created to compute one number. Nothing in the code looks like a loop — the memory of what to finish next is the stack of paused frames, and the base case is the only reason that stack ever stops growing.

Predict before you check

function f(n) {
  if (n <= 1) return n;
  return f(n - 1) + f(n - 2);
}

f(4)

Without running it — what does f(4) return?

Getting the right answer here doesn't mean you understand recursion. It means you can simulate a call stack once, carefully, under no time pressure. Vectra checks whether you can do it on a version you haven't seen — that's the difference between recognizing recursion and being able to use it.

The gap underneath the gap

Almost everyone who says 'I understand recursion but this problem confuses me' is actually missing something one level down: tracing a single call's stack frame all the way through — what it's waiting on, and what it does the instant that answer comes back. Recursion is just that, repeated. If the frame-by-frame unwind is fuzzy, no amount of re-reading the recursion definition will fix it — you're studying the wrong layer.

it doesn't move on until you get it.

Not ready to sign up? Name any concept and Vectra will show you what it sits on — for recursion that is usually the stack-frame unwind, not the definition you have already read twice.

See what recursion sits on, free →

Sources & review

  • Learner quote: verbatim Stack Exchange question (568,773 views when collected), via Vectra's recorded problem corpus.
  • Every value in the diagram and worked example hand-derived from the code shown on this page; f(4) = 3 re-checked 2026-10-14.

Reviewed 2026-10-14