# def Define a function. ## Syntax
def name { |params| body }
def name { |param^type| body }    # with type hints
def name { || body }              # zero params
## Examples
def greet {|name|
    echo "Hello, $name!"
}
[greet "Alice"]
def add {|a b| ($a + $b) }
[add 3 4]   # 7
# Type-hinted parameters
def double {|x^number| ($x * 2) }
[double 5]   # 10
# Recursive function
def fib {|n|
    if ($n < 2) { $n } else {
        ([fib ($n - 1)] + [fib ($n - 2)])
    }
}
[fib 10]   # 55
# Multi-head form — the same function as guarded clauses,
# checked top to bottom; first matching clause wins
def fib {
    {|n| $n } when ($n < 2)
    {|n| ([fib ($n - 1)] + [fib ($n - 2)]) }
}
[fib 10]   # 55
## Parameters | Parameter | Description | |-----------|-------------| | `name` | Function name | | `\|params\|` | Parameter list between bars; use `^type` for type hints | | `body` | Function body | | `\|\|` | Empty parameter list (zero params) | ## Notes - Functions must be defined before they are called (top-to-bottom evaluation) - Lambda blocks use `$it` as implicit single argument: `{ ($it * 2) }` - Named lambda params: `{ |acc it| ($acc + $it) }` - Use `return` for early exit from a function ## See Also `deftype`, `defmethod`, `defprotocol`, `return`