Skip to content

Functions

Functions are declared with function. They take zero or more typed parameters and optionally return a typed value.

munos
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.

munos
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)  // explicit

Named 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:

munos
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 edge

Defaults 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 function can'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.

munos
var total = add(2, 3)   // ok — the result is a value
// var f = add          // error — a function is not a value

One 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.

Part of the MultiNostalgia project.