การเรนเดอร์th-TH

functio

Translation status: ภาษาไทย reader-locale proof. Code fences render through the th-TH pipeline; prose is canonical Latin.

Declares a named function or method.

Aliases: function

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

Category#

function

Examples#

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

Declares a named function or method.

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

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

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

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

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

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

main {
    saluta()

    dic("Bonum diem!")

    const _ rex  nomen()
    print rex

    print duplica(21)
}

Expected output:

Salve, Mundus!
Bonum diem!
Marcus Aurelius
42

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

Declares a named function or method.

# =============================================================================
# functio — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
#
# Common mistakes:
#   • Using a value after it has been moved by `ex` — `ex` consumes ownership; accessing the value after a move is a use-after-move error (SEM050).
#
# See also: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
# =============================================================================

# Parameter borrow markers: de (shared), in (mutable), ex (consume)
#
# de <type> <nomen>  -- shared borrow (read-only from caller's view)
# in <type> <nomen>  -- mutable borrow (callee may modify the binding)
# ex <type> <nomen>  -- consume/move ownership into callee
#
# GRAMMAR:
#   parameter :← ('de' | 'in' | 'ex')? type ident ...
#
# EXPECTED OUTPUT:
#   mutabile
#   6
#   salve
#
# BACKEND: Rust lowering treats `in` as immutable args (borrow lowering gap;
# whitelist: functio/in-ex.fab). Go target is runnable.

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

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

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

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

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

fn consume(from string buffer)  string {
    return buffer
}

main {
    imprime("mutabile")

    var int value  3
    # in borrows valor mutably inside duplica
    duplica(value)

    # ex moves the literal into consume
    print consume("salve")
}

Expected output:

mutabile
6
salve

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

Declares a named function or method.

# =============================================================================
# functio — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
#
# Common mistakes:
#   • Omitting a base case — recursive functions without a terminating condition cause infinite recursion and stack overflow.
#
# See also: →, ⇥, redde, sponte, ceteri, prae, curata, futura, 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 {
        return 0
    }
    if n  1 {
        return 1
    }
    return fibonacci(n - 1) + fibonacci(n - 2)
}

# Sum from 1 to n
fn summatio(int n)  int {
    if n  0 {
        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/functio/sponte-vel.fab (canonical · keyword)#

Declares a named function or method.

# =============================================================================
# functio — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
#
# Common mistakes:
#   • Forgetting that `sponte` parameters produce `T ∪ nihil` — accessing a `sponte` value without checking `est nihil` first may fail.
#
# See also: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
# =============================================================================

# Parameters with sponte and vel
#
# sponte marks a voluntary (optional) parameter slot
# vel provides a default when the argument is omitted
#
# GRAMMAR:
#   parameter :← (preposition)? type nomen sponte? ('ut' 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 nomen, string titulus optional)  string {
    if titulus is null {
        return "Salve, §!"(nomen)
    }
    return "Salve, § §!"(titulus, nomen)
}

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 null {
        # de = shared borrow of fons
        return fons.longitudo()
    }
    return altitudo
}

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

main {
    print saluta("Marcus")

    print saluta("Marcus", "Dominus")

    # both sponte 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)
}

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

Declares a named function or method.

# =============================================================================
# functio — Declares a named function or method
# =============================================================================
#
# What this teaches:
#   • Declares a named function or method.
#   • Related keywords: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
#
# Common mistakes:
#   • Reversing type and name — Faber uses type-first syntax (`Type name`), not name-then-type (`name: Type`).
#
# See also: →, ⇥, redde, sponte, ceteri, prae, curata, futura, cursor
# =============================================================================

# Functions with typed parameters (type-first syntax)
#
# functio <nomen>(<type> <param>, ...) → <type> { <body> }
#
# GRAMMAR:
#   funcDecl :← 'functio' ident '(' paramList ')' ('→' type)? block
#
# EXPECTED OUTPUT:
#   49, 300, "Julius habet 30 annos", falsum, verum, 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 nomen, int aetas)  string {
    return "§ habet § annos"(nomen, aetas)
}

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

# fractus (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
falsum
verum
5.0