Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

fn

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Declares a named function or method.

Aliases: function

Syntax: fn <name>(<params>) [modifiers] [→ <type>] [⇥ <error-type>] <block>

Category#

function

Examples#

radix/corpus/reference/keyword/functio/functio.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# fn — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, return, optional, rest, prae, future, cursor
#
# Common mistakes:
#   • Omitting the `→ T` return type annotation — if a function uses `return`, the return type must be declared; `return` outside a function body is also an error (SEM032).
#
# See also: →, ⇥, return, optional, rest, prae, future, cursor
# =============================================================================

# Basic function declarations
#
# fn <name>() { <body> }
# fn <name>() → <type> { <body> }
#
# GRAMMAR:
#   funcDecl :← 'fn' ident '(' paramList ')' ('→' type)? block
#
# EXPECTED OUTPUT:
#   functio.expected — four diagnostics (greetings, name, integer).

# Function with no parameters, no return
fn saluta() → void {
    print "Salve, Mundus!"
}

# Function with parameter, no explicit return type
fn dic(string verbum) → void {
    print verbum
}

# Function with return type
fn name() → string {
    return "Marcus Aurelius"
}

# Function with parameter and return type
fn duplica(int n) → int {
    return n * 2
}

main {
    saluta()
    dic("Bonum diem!")
    let string rex ← name()
    print rex
    print duplica(21)
}

Expected output:

Salve, Mundus!
Bonum diem!
Marcus Aurelius
42

radix/corpus/reference/keyword/functio/in-ex.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# fn — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, return, optional, rest, prae, future, cursor
#
# Common mistakes:
#   • Using a value after it has been moved by `from` — `from` consumes ownership; accessing the value after a move is a use-after-move error (SEM050).
#
# See also: →, ⇥, return, optional, rest, prae, future, cursor
# =============================================================================

# Parameter access markers live in the type position:
#
# ref <type> <name>  -- shared read-only view
# mut <type> <name>  -- mutable borrow; writes reach the caller
# own <type> <name>  -- consume ownership into the callee
# copy <type> <name>  -- duplicate at acquisition, then own the copy
#
# The old parameter-prefix slot is retired. The `from`/`from` token identity
# remains available for imports, iteration, and field extraction.
#
# EXPECTED OUTPUT:
#   mutabile
#   6
#   salve
#
# BACKEND: Rust emission propagates the scalar mutable borrow as `&mut i64`.
# The MIR runner preserves that scalar borrow during execution and writes the
# updated value back to the caller; MU-5b-R1 closed the former gap.

# --- Shared borrow: read label without taking ownership ---

fn imprime(ref string label) → void {
    print label
}

# --- Mutable borrow: double the caller's int in place ---

fn duplica(mut int value) → void {
    value ← value * 2
    print value
}

# --- Consume: take ownership of string and return it ---

fn consume(own string buffer) → string {
    return buffer
}

main {
    imprime("mutabile")
    mut int value ← 3
    # mut borrows the caller's scalar inside duplica
    duplica(mut value)

    # own consumes the literal into consume
    print consume(own "salve")
}

Expected output:

mutabile
6
salve

radix/corpus/reference/keyword/functio/recursio.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# fn — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, return, optional, rest, prae, future, cursor
#
# Common mistakes:
#   • Omitting a base case — recursive functions without a terminating condition cause infinite recursion and stack overflow.
#
# See also: →, ⇥, return, optional, rest, prae, future, cursor
# =============================================================================

# Recursive functions
#
# A function may call itself; every recursion path needs a base case.
#
# GRAMMAR:
#   funcDecl with callExpr naming the enclosing function
#
# EXPECTED OUTPUT:
#   factorial: 1, 1, 120, 3628800
#   fibonacci: 0, 1, 55
#   summatio: 15, 55

# Factorial: n! ← n * (n-1)!
fn factorial(int n) → int {
    if n ≤ 1 {
        # base case
        return 1
    }
    return n * factorial(n - 1)
}

# Fibonacci: fib(n) ← fib(n-1) + fib(n-2)
fn fibonacci(int n) → int {
    if n ≤ 0 then return 0
    if n ≡ 1 then return 1
    return fibonacci(n - 1) + fibonacci(n - 2)
}

# Sum from 1 to n
fn summatio(int n) → int {
    if n ≤ 0 then return 0
    return n + summatio(n - 1)
}

main {
    # --- Factorial examples ---

    print factorial(0)
    print factorial(1)
    print factorial(5)
    print factorial(10)

    # --- Fibonacci examples ---

    print fibonacci(0)
    print fibonacci(1)
    print fibonacci(10)

    # --- Summation examples ---

    print summatio(5)
    print summatio(10)
}

Expected output:

1
1
120
3628800
0
1
55
15
55

radix/corpus/reference/keyword/functio/sponte-vel.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# fn — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, return, optional, rest, prae, future, cursor
#
# Common mistakes:
#   • Forgetting that `optional` parameters produce `T ∪ none` — accessing a `optional` value without checking `est none` first may fail.
#
# See also: →, ⇥, return, optional, rest, prae, future, cursor
# =============================================================================

# Parameters with optional and vel
#
# optional marks a voluntary (optional) parameter slot
# vel provides a default when the argument is omitted
#
# GRAMMAR:
#   parameter :← (preposition)? type name optional? ('as' alias)? ('vel' defectum)?
#
# EXPECTED OUTPUT:
#   Salve, Marcus!, Salve, Dominus Marcus!, pagina lines with defaults/overrides,
#   codex length and metire results, civis summary strings

fn saluta(string name, string titulus optional) → string {
    if titulus is none then return "Salve, §!"(name)
    return "Salve, § §!"(titulus, name)
}

fn pagina(int pagina optional coalesce 1, int quantitas optional coalesce 10) → string {
    return "pagina § cum § rebus"(pagina, quantitas)
}

fn metire(string fons, ref int altitudo optional) → int {
    if altitudo is none {
        # ref = shared borrow of fons
        return fons.length()
    }
    return altitudo
}

fn civis(string name, int aetas optional coalesce 0, bool activus optional coalesce true) → string {
    return "civis: §, aetas: §, activus: §"(name, aetas, activus)
}

main {
    print saluta("Marcus")
    print saluta("Marcus", "Dominus")

    # both optional params use vel defaults
    print pagina()
    print pagina(2, 25)

    # quantitas defaults to 10
    print pagina(5)
    print metire("codex")
    print metire("codex", 5)
    print civis("Julia")
    print civis("Julia", 25)
    print civis("Julia", 25, false)
}

Expected output:

Salve, Marcus!
Salve, Dominus Marcus!
pagina 1 cum 10 rebus
pagina 2 cum 25 rebus
pagina 5 cum 10 rebus
5
5
civis: Julia, aetas: 0, activus: true
civis: Julia, aetas: 25, activus: true
civis: Julia, aetas: 25, activus: false

radix/corpus/reference/keyword/functio/typi-parametri.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# fn — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, return, optional, rest, prae, future, cursor
#
# Common mistakes:
#   • Reversing type and name — Faber uses type-first syntax (`Type name`), not name-then-type (`name: Type`).
#
# See also: →, ⇥, return, optional, rest, prae, future, cursor
# =============================================================================

# Functions with typed parameters (type-first syntax)
#
# fn <name>(<type> <param>, ...) → <type> { <body> }
#
# GRAMMAR:
#   funcDecl :← 'fn' ident '(' paramList ')' ('→' type)? block
#
# EXPECTED OUTPUT:
#   49, 300, "Julius habet 30 annos", false, true, 5.0

# Single typed parameter
fn quadratum(int n) → int {
    return n * n
}

# Multiple typed parameters
fn adde(int a, int b) → int {
    return a + b
}

# Mixed types for string formatting
fn narra(string name, int aetas) → string {
    return "§ habet § annos"(name, aetas)
}

# Boolean parameter and return
fn nega(bool value) → bool {
    return not value
}

# float (floating-point) parameters
fn media(float a, float b) → float {
    return (a + b) / 2.0
}

main {
    print quadratum(7)
    print adde(100, 200)
    print narra("Julius", 30)
    print nega(true)
    print nega(false)
    print media(3.0, 7.0)
}

Expected output:

49
300
Julius habet 30 annos
false
true
5.0

radix/corpus/reference/reject/functio/fallthrough-reject.fab (supporting · reject)#

A value-returning function must return on every path; a guard that leaves a fall-through path is an error.

# =============================================================================
# fn — every path of a value-returning function returns
# =============================================================================
# This exemplum intentionally fails to compile: the guard returns on one path
# and falls off the end of the function on the other.

fn f(int a) → int {
    if a ≻ 0 {
        return 1
    }
}

main {
    print f(1)
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/functio/inline-comment-unfinished-expression-reject.fab (supporting · reject)#

A # comment after an operator inside an unfinished expression is refused.

fn value() → int {
    let int answer ← 1 + # unfinished expression
        2
    return answer
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/functio/wrong-arg-count-reject.fab (supporting · reject)#

A call must pass exactly the declared number of arguments; extra arguments are an error.

# =============================================================================
# fn — a call passes one argument per parameter
# =============================================================================
# This exemplum intentionally fails to compile: `f` takes one parameter and
# the call supplies two.

fn f(int a) → int {
    return a
}

main {
    print f(1, 2)
}

Expected: compilation rejects this example.