# How does the fibonacci recursive function “work”?

You’re defining a function in terms of itself. In general, `fibonnaci(n) = fibonnaci(n - 2) + fibonnaci(n - 1)`. We’re just representing this relationship in code. So, for `fibonnaci(7)` we can observe:

• `fibonacci(7)` is equal to `fibonacci(6)` + `fibonacci(5)`
• `fibonacci(6)` is equal to `fibonacci(5)` + `fibonacci(4)`
• `fibonacci(5)` is equal to `fibonacci(4)` + `fibonacci(3)`
• `fibonacci(4)` is equal to `fibonacci(3)` + `fibonacci(2)`
• `fibonacci(3)` is equal to `fibonacci(2)` + `fibonacci(1)`
• `fibonacci(2)` is equal to `fibonacci(1)` + `fibonacci(0)`
• `fibonacci(1)` is equal to 1
• `fibonacci(0)` is equal to 1

We now have all the parts needed to evaluate `fibonacci(7)`, which was our original goal. Notice that the base case`return 1` when `n < 2` — is what makes this possible. This is what stops the recursion, so that we can can start the process of unrolling the stack and summing the values we’re returning at each step. Without this step, we’d continue calling `fibonacci` on smaller and smaller values right up until the program finally crashed.

It might help to add some logging statements that illustrate this:

``````function fibonacci(n, c) {
var indent = "";
for (var i = 0; i < c; i++) {
indent += " ";
}
console.log(indent + "fibonacci(" + n + ")");
if (n < 2) {
return 1;
} else {
return fibonacci(n - 2, c + 4) + fibonacci(n - 1, c + 4);
}
}

console.log(fibonacci(7, 0));
``````

Output:

``````fibonacci(7)
fibonacci(5)
fibonacci(3)
fibonacci(1)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(4)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(3)
fibonacci(1)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(6)
fibonacci(4)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(3)
fibonacci(1)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(5)
fibonacci(3)
fibonacci(1)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(4)
fibonacci(2)
fibonacci(0)
fibonacci(1)
fibonacci(3)
fibonacci(1)
fibonacci(2)
fibonacci(0)
fibonacci(1)
``````

Values at the same level of indentation are summed to produce the result for the previous level of indentation.