Skip to content

Functions and Calls

Chloë Strainge edited this page Oct 15, 2026 · 2 revisions

Functions and calls

Functions use ordinary shell-style calls while retaining typed arguments and results. Printed output is independent of the result returned to the caller.

fn area($width: Float, $height: Float): Float { $width * $height }
let $size = area 3 2.5
echo $size (area 2 4)
7.5 8.0

Parameters and results can be annotated. Integer arguments widen where Float is required. Other conversions are explicit. A final expression supplies the result; return value supplies it early. A trailing newline or semicolon does not discard that result. Bare return and an empty body produce ().

Omitting the return annotation means : (): the function must produce unit. Use : any for a function whose result may have any type, or declare the required result type. This rule also applies to methods, anonymous functions, and function prototypes. Unannotated parameters still accept any value.

fn announce($text: String)
{
    echo $text
    ()
}
fn identity($value): any { $value }
fn answer(): Integer { 42 }
echo (announce "Ready") (identity "memo") (answer)
Ready
() memo 42

A final command can return ExecResult. The explicit () above ends the function with unit after printing. The output text is separate from the function’s value. A known incompatible result is rejected before execution. Results whose type is only known at runtime are checked when the function returns. return value obeys the same contract as a trailing expression.

A word depends on its position

Form Meaning
work Call without arguments
let $x = work Call; unresolved command is a runtime error
let $x = "work" Store text
echo work Pass the word as an argument
echo (work) Call, then pass the result
let $f = `work Validate and store a reference without calling
$f 7 Call a stored reference with one argument

Parentheses contain one expression, not a list of statements. Empty parentheses in argument position supply unit: work () is a one-argument call.

Optional and variadic parameters

fn ticket($name: String, $zone: String | ()): String
{
    let $area = if $zone == () { "general" } else { $zone }
    $name + ": " + $area
}
fn total($values: Integer...): Number { $values.sum }
echo (ticket "Mira") (total 4 6 8)
Mira: general 18

Trailing T | () parameters may be omitted from right to left. A required parameter cannot follow an omittable one. A final $values: T... collects extra arguments into [T]; it receives an empty array when no extras remain. Use ... to expand an existing array into individual arguments.

References and versions

Backticks validate at runtime. A missing target, inaccessible module function, or non-executable file is an error at reference creation. Scripted function references retain their selected version. Later redefinitions affect newly compiled code, not an existing reference. External names remain resolved through PATH when called.

Known compiled calls preserve the definitions they selected too. Forward functions are registered for the submitted source before execution. Anonymous functions add live captured variables; named functions otherwise use active caller-variable scope.


Related: Closures and function types · Commands

Clone this wiki locally