Functions
Functions are declared with function. They take zero or more typed parameters and optionally return a typed value.
function add(a: i32, b: i32): i32 {
return a + b
}
function greet(name: string) {
print("hello ", name) // no return type → returns nothing
}Default parameters
A parameter may have a default value, letting callers omit it. Defaults are restricted to trailing parameters — once one parameter has a default, every parameter after it must too. Default expressions must be compile-time constants.
function drawRect(x: i32, y: i32, w: i32, h: i32, color: u32 = 0x00FFFFFF) {
// ...
}
drawRect(10, 20, 32, 32) // color defaults
drawRect(10, 20, 32, 32, 0x00FF0000) // explicitNamed arguments
Arguments may be passed by parameter name with name = value. Rules:
- Positional first, named after.
- No double-binding — a parameter can't be both positional and named in one call.
- All non-defaulted parameters must be supplied (positionally or by name); defaulted ones may be omitted.
Named arguments are how you skip a defaulted parameter while overriding a later one — common with draw_image's long optional tail:
draw_image(sprite, pal, 10, 20) // all defaults
draw_image(sprite, pal, 10, 20, flip_y = true) // skip flip_x, set flip_y
draw_image(sprite, pal, 10, 20, clip_rect_y1 = 32) // set just one scissor edgeDefaults and named arguments are purely compile-time — every call site resolves to a fully positional argument list with no runtime cost.
Top-level only, and hoisted
- Function declarations are top-level only. A
functioncan't be nested inside another function or an event. The function table is flat and script-wide. - Functions are hoisted. A function is callable anywhere, regardless of where it's declared in the source. (
vars are not hoisted.)
Functions are not first-class
A function name is not a value. You cannot assign a function to a variable, pass it as an argument, store it, or return it. Only the result of a call is a value.
var total = add(2, 3) // ok — the result is a value
// var f = add // error — a function is not a valueOne exception
The callback slot of an async builtin (like db_get) accepts a top-level function name or an inline function literal. That's a builtin-level facility, not general first-class functions — see Persistence.