Skip to content
Justice Tech School

Recursion

A method can call itself. Add a base case.

Example
def fact(n) = n <= 1 ? 1 : n * fact(n - 1)
def fib(n) = n < 2 ? n : fib(n - 1) + fib(n - 2)
puts fact(5), fib(10)

Result

120
55

Exercise

Base case.

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.