← Back to Docs

Return Values

return sends a value back from a function to the caller.

Syntax

return expr;    // return a value
return;         // return void

Returning a value

class App {
    fn add(a, b) {
        return a + b;
    }

    let result = add(3, 4);
    print(result);    // 7
}

Early return

return exits the function immediately, skipping any remaining code:

class App {
    fn abs(n) {
        if (n >= 0) {
            return n;
        }
        return 0 - n;
    }

    print(abs(5));     // 5
    print(abs(0 - 3)); // 3
}

Return with conditionals

class App {
    fn max(a, b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

    print("max:", max(10, 20));    // max: 20
}

Void return

Functions without a return statement (or with a bare return;) return void:

class App {
    fn doWork() {
        print("working...");
        // no return — returns void
    }

    let result = doWork();
    print(result);    // void
}

Using return values in expressions

Return values can be used anywhere an expression is expected:

class App {
    fn square(n) { return n * n; }
    fn double(n) { return n * 2; }

    print(square(3) + double(4));    // 9 + 8 = 17
    let x = square(square(2));       // square(4) = 16
}