函数
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.
Declares a named function or method.
Aliases: function
Syntax: 函数 <name>(<params>) [modifiers] [→ <type>] [⇥ <error-type>] <block>
Category#
function
Related#
Examples#
radix/corpus/functio/functio.fab (canonical · keyword)#
Declares a named function or method.
# =============================================================================
# 函数 — Declares a named function or method
# =============================================================================
#
# What this teaches:
# • Declares a named function or method.
# • Related keywords: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
#
# Common mistakes:
# • Omitting the `→ T` return type annotation — if a function uses `返回`, the return type must be declared; `返回` outside a function body is also an error (SEM032).
#
# See also: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
# =============================================================================
# Basic function declarations
#
# 函数 <名称>() { <body> }
# 函数 <名称>() → <type> { <body> }
#
# GRAMMAR:
# funcDecl :← '函数' 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 名称() → string {
return "Marcus Aurelius"
}
# Function with parameter and return type
fn duplica(int n) → int {
return n * 2
}
main {
saluta()
dic("Bonum diem!")
const string rex ← 名称()
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.
# =============================================================================
# 函数 — Declares a named function or method
# =============================================================================
#
# What this teaches:
# • Declares a named function or method.
# • Related keywords: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
#
# Common mistakes:
# • Using a value after it has been moved by `取自` — `取自` consumes ownership; accessing the value after a move is a use-after-move error (SEM050).
#
# See also: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
# =============================================================================
# Parameter access markers live in the type position:
#
# ref <type> <名称> -- shared read-only view
# mut <type> <名称> -- mutable borrow; writes reach the caller
# own <type> <名称> -- consume ownership into the callee
# 拷贝 <type> <名称> -- duplicate at acquisition, then own the 拷贝
#
# The old parameter-prefix slot is retired. The `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 整数 in place ---
fn duplica(mut int value) → void {
value ← value * 2
print value
}
# --- Consume: take ownership of 文本 and return it ---
fn consume(拥有 string buffer) → string {
return buffer
}
main {
imprime("mutabile")
var int value ← 3
# mut borrows the caller's scalar inside duplica
duplica(value)
# own consumes 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.
# =============================================================================
# 函数 — Declares a named function or method
# =============================================================================
#
# What this teaches:
# • Declares a named function or method.
# • Related keywords: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
#
# Common mistakes:
# • Omitting a base case — recursive functions without a terminating condition cause infinite recursion and 栈 overflow.
#
# See also: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
# =============================================================================
# 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/functio/sponte-vel.fab (canonical · keyword)#
Declares a named function or method.
# =============================================================================
# 函数 — Declares a named function or method
# =============================================================================
#
# What this teaches:
# • Declares a named function or method.
# • Related keywords: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
#
# Common mistakes:
# • Forgetting that `可选` parameters produce `T ∪ 空类型` — accessing a `可选` value without checking `est 空类型` first may fail.
#
# See also: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
# =============================================================================
# Parameters with 可选 and vel
#
# 可选 marks a voluntary (optional) parameter slot
# vel provides a default when the argument is omitted
#
# GRAMMAR:
# parameter :← (preposition)? type 名称 可选? ('作为' 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 名称, string titulus optional) → string {
if titulus is none then return "Salve, §!"(名称)
return "Salve, § §!"(titulus, 名称)
}
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 {
# 借自 = shared borrow of fons
return fons.length()
}
return altitudo
}
fn civis(string 名称, int aetas optional coalesce 0, bool activus optional coalesce true) → string {
return "civis: §, aetas: §, activus: §"(名称, aetas, activus)
}
main {
print saluta("Marcus")
print saluta("Marcus", "Dominus")
# both 可选 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.
# =============================================================================
# 函数 — Declares a named function or method
# =============================================================================
#
# What this teaches:
# • Declares a named function or method.
# • Related keywords: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
#
# Common mistakes:
# • Reversing type and name — Faber uses type-first syntax (`Type name`), not name-then-type (`name: Type`).
#
# See also: →, ⇥, 返回, 可选, 其余, prae, 未来, 迭代器
# =============================================================================
# Functions with typed parameters (type-first syntax)
#
# 函数 <名称>(<type> <param>, ...) → <type> { <body> }
#
# GRAMMAR:
# funcDecl :← '函数' ident '(' paramList ')' ('→' type)? block
#
# EXPECTED OUTPUT:
# 49, 300, "Julius habet 30 annos", 假, 真, 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 名称, int aetas) → string {
return "§ habet § annos"(名称, aetas)
}
# Boolean parameter and return
fn nega(bool value) → bool {
return not value
}
# 小数 (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