← Back to Docs
Functions (fn)
Functions are declared with fn and can take parameters and return values.
Syntax
fn name(param1, param2) {
// body
}Basic function
class App {
fn sayHello() {
print("Hello!");
}
sayHello();
}Parameters
class App {
fn greet(name) {
print("Hello,", name);
}
greet("Alice"); // Hello, Alice
greet("Bob"); // Hello, Bob
}Return values
Use return to send a value back from a function. See Return Values.
class App {
fn add(a, b) {
return a + b;
}
let result = add(3, 4);
print(result); // 7
}Functions as expressions
Function calls can be used anywhere an expression is expected:
class App {
fn double(n) {
return n * 2;
}
print(double(5)); // 10
print(double(3) + 1); // 7
let x = double(double(2)); // 8
}Hoisting
Functions are registered before any statements run (two-pass execution). You can call a function before it appears in the code:
class App {
sayHi(); // works!
fn sayHi() {
print("hi");
}
}