العرضar

دالة

Translation status: العربية reader-locale proof. Term names and code fences follow the ar 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

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, مستقبل, cursor
#
# 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, مستقبل, cursor
# =============================================================================

# 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, مستقبل, cursor
#
# 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, مستقبل, cursor
# =============================================================================

# 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, مستقبل, cursor
#
# Common mistakes:
#   • Omitting a base case — recursive functions without a terminating condition cause infinite recursion and مكدس overflow.
#
# See also: →, ⇥, أعد, اختياري, باقي, prae, مستقبل, 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/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, مستقبل, cursor
#
# Common mistakes:
#   • Forgetting that `اختياري` parameters produce `T ∪ نوع_لاشيء` — accessing a `اختياري` value without checking `est نوع_لاشيء` first may fail.
#
# See also: →, ⇥, أعد, اختياري, باقي, prae, مستقبل, cursor
# =============================================================================

# 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, مستقبل, cursor
#
# Common mistakes:
#   • Reversing type and name — Faber uses type-first syntax (`Type name`), not name-then-type (`name: Type`).
#
# See also: →, ⇥, أعد, اختياري, باقي, prae, مستقبل, cursor
# =============================================================================

# 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