# Recursive function as block value

I want to add a simple recursive function as a cell value. Let’s say

``````fib = function(n)  {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
``````

– but Observable complains about circular definition.

I understand that Observable needs to evaluate `fib` in a reactive way, and it cannot resolve the circular definition. I could work around that by specifiying:

``````fib = function(n)  {
function _fib(n) {
if (n == 0) return 0;
if (n == 1) return 1;
return _fib(n - 1) + _fib(n - 2);
}

return _fib(n)
}
``````

… but I would like to avoid that for didactic purposes (this is for a tutorial directed towards newcomers).

I briefly looked into the `mutable` keyword, but I feel like I would need the opposite, like an `immutable` keyword that breaks reactive evaluation.

Any suggestion for how to get this to work without introducing too much semantic noise that unnecessarily confuses newcomers/students?

2 Likes

Sure! The simplest way to do this one is:

``````function fib(n)  {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
``````

Observable pulls out the name from function declarations (`function ___`) and class declarations (`class ___`), so they work without needing an additional `=` - other cells can reference `fib` by name.

6 Likes