API Reference › Program structure › function
keyword

function

function name(a: T, b: T) -> R { … }

A function declares a function: a reusable block of logic with typed parameters and a return type, written -> R (use -> void for one that returns nothing). Call it positionally, heal(2, 10), or with named arguments, heal(amount: 2, maximum: 10), which reads more clearly at the call site and is the house style. Functions live at program scope alongside handlers and may be called from any handler; they can spawn, run queries, and read program-scope state. Use them to factor out logic shared by several handlers so each handler stays short.

Example

program Healer {
  property Health { current: int = 100, maximum: int = 100 }
  model Player { Health }

  function heal(amount: int, maximum: int) -> int {
    let restored = amount * 2
    if restored > maximum { return maximum }
    return restored
  }

  handler Boot phase Start {
    spawn Hero { Health { current: 10, maximum: 100 } }
  }

  handler Recover phase Update {
    for (health) in query [Health, {Player}] {
      health.current = heal(amount: health.current, maximum: health.maximum)
    }
  }
}
← All symbols