Skip to content

Parameters and Returns

Function Parameters

Parameters are variables that receive values when a function is called.

fn greet(name, age) {
    print "Hello #{name}, you are #{age} years old";
}

greet("Alice", 30);

Return Values

Functions can return values using the return keyword.

fn calculate_total(price, quantity) {
    let total = price * quantity;
    return total;
}

let cost = calculate_total(10.99, 3);
print cost;  // 32.97

Multiple Return Points

fn get_discount(age) {
    if age < 18 {
        return 0.50;  // 50% youth discount
    }
    if age >= 65 {
        return 0.30;  // 30% senior discount
    }
    return 0.10;  // 10% regular discount
}

print get_discount(15);  // 0.5
print get_discount(70);  // 0.3
print get_discount(30);  // 0.1