ludic/docs/language/structure/kw-function.md
Orkuncakilkaya 4c48077d68 refactor(lang): rename the fn keyword to function
Expand the function-declaration keyword to the full word across the whole
language and toolchain:
  fn name(...) -> T { ... }   ->   function name(...) -> T { ... }

Done as a self-hosting migration: teach the parser both spellings, reseed,
rewrite every .ludic definition to `function`, then drop `fn`. The compiler
now rejects `fn`. Touches the parser, all selfhost/tools/runtime/example/test
sources, the grammars (TextMate shared+vscode, ludic_syntax.h, JetBrains
LudicTokens.kt), the LSP and formatter, the Python doc/vocab tools
(check-impl, check-docs, validate, palette, test-lsp), and the docs
(fences, prose, kw-fn -> kw-function).

Reseeded; C-free bootstrap fixpoint holds. All suites green (45 regression,
24 self-host, 29 tool); the docs site generates and check.py passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 01:43:22 +03:00

1.4 KiB


id: kw-function name: function category: structure kind: keyword tokens: function sig: function name(a: T, b: T) -> R { … } tip: A function — reusable logic called positionally or with named arguments. order: 8

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.

Parameters:

  • a, b — the typed inputs; pass them positionally or by name at the call site
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)
    }
  }
}