Skip to content
Justice Tech School

Recursion

A function may call itself with a base case.

Example
fact <- function(n) if (n <= 1) 1 else n * fact(n - 1)
fib <- function(n) if (n < 2) n else fib(n - 1) + fib(n - 2)
print(fact(5)); print(fib(10))

Exercise

Base case.

if (n <= ) 1

Quiz Test

What stops recursion?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.