# Recursive function as block value

**URL:** <https://talk.observablehq.com/t/recursive-function-as-block-value/734>\
**Category:** Help\
**Created:** [May 9, 2018, 6:33am UTC](https://talk.observablehq.com/t/recursive-function-as-block-value/734 "2018-05-09T06:33:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![floledermann](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/floledermann/32/566_2.png) [@floledermann](https://talk.observablehq.com/u/floledermann)\
**Post date:** [May 9, 2018, 6:33am UTC](https://talk.observablehq.com/t/recursive-function-as-block-value/734/1 "2018-05-09T06:33:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tom/32/2262_2.png) [@tom](https://talk.observablehq.com/u/tom)\
**Post date:** [May 9, 2018, 7:03am UTC](https://talk.observablehq.com/t/recursive-function-as-block-value/734/2 "2018-05-09T07:03:56Z")

</div>

Sure! The simplest way to do this one is:

```auto
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.
