B4Coder

🪆 Recursion · Mission 5 of 5

🐚 Fibonacci

0, 1, 1, 2, 3, 5, 8, 13… each number is the two before it added up. A function that calls itself twice can find any of them.

Your mission

  1. Make a Function fib, given n. If n <= 1, Return n.
  2. Otherwise Call fib(n - 1) into a, Call fib(n - 2) into b, and Return a + b.
  3. Main: Ask for n, Call fib(n) into answer, and Tell "fib(" + n + ") = " + answer. Step through a small one and watch the stack.

Getting the mission ready…