Larzscript Course · Lesson 4 of 15

Functions & Recursion

Larz covers how Larzscript packages logic into functions: defining them with fn, handing a value back with return, and recursion โ€” a function calling itself, with a base case to stop it.

fn and return

fn, a name, parameters in parentheses, then a block. Call it the same way you'd call print. return hands a value back to whoever called it:

fn greet(name) {
    print(f"Hello, {name}!")
}

greet("Larz")

fn add(a, b) {
    return a + b
}

print(add(2, 3))
Output
Hello, Larz!
5

recursion

A function can call itself โ€” that's recursion. Every recursive function needs a base case: a condition where it stops calling itself and just returns a value directly:

fn factorial(n) {
    if n <= 1 {
        return 1
    }
    return n * factorial(n - 1)
}

print(factorial(5))

fn fib(n) {
    if n < 2 {
        return n
    }
    return fib(n - 1) + fib(n - 2)
}

print(fib(7))
Output
120
13

Every snippet on this page is run through the real larzscript interpreter, not hand-typed โ€” what you see in the Output blocks is the actual output, for everything shown: defining a function, returning a value, and a function calling itself recursively.

← Lesson 3Course syllabusGitHub