渲染zh-Hant

傳回

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Function returning a string literal via 傳回 \"文字\".

Aliases: return

Syntax: 傳回 <string-literal>

Category#

transfer

Examples#

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

Returns a value from a function.

# =============================================================================
# 傳回 — Returns a value from a function.
# =============================================================================
#
# What this teaches:
#   • Function return — `傳回 <expression>` returns a value from a `函式`,
#     and bare `傳回` returns `空值`
#   • Early returns with `若 <cond> 則 傳回 <expr>` for guard clauses
#
# Common mistakes:
#   • Using `傳回` outside a function body — `傳回` is only valid inside `函式` declarations (SEM032).
#
# See also: →, ⇥, 則, 拋出, 靜默
# =============================================================================

# 傳回 statements (function return)
#
# 傳回 <expression>   -- return a value
# 傳回                -- return void (空值)
#
# GRAMMAR:
#   returnStmt :← '傳回' expr?
#
# EXPECTED OUTPUT:
#   30, Salve, Munde, 42, 0, 20

fn adde(int a, int b) → int {
    return a + b
}

fn saluta(string 名稱) → string {
    return "Salve, " + 名稱
}

fn duoetquadraginta() → int {
    return 42
}

fn tace() → void {
    # bare 傳回 for 空值 return type
    return
}

fn porta(int x) → int {
    if x ≺ 0 {
        # early return on negative input
        return 0
    }
    return x * 2
}

main {
    print adde(10, 20)
    print saluta("Munde")
    print duoetquadraginta()
    tace()
    print porta(-5)
    print porta(10)
}

Expected output:

30
Salve, Munde
42
0
20

radix/corpus/scalar/return-bool.fab (canonical · keyword)#

Functions returning boolean literals via 傳回 真 and 傳回 假.

# =============================================================================
# 傳回 — Functions returning boolean literals via 傳回 真 and 傳回 假.
# =============================================================================
#
# What this teaches:
#   • Boolean return values — `傳回 真` and `傳回 假` are valid return
#     expressions for `布林`-typed functions
#   • Demonstrates that boolean literals pass through the return statement
#     unchanged
#
# Common mistakes:
#   • Confusing a scalar (single-value) return with a collection return — this function returns one `布林`, not a list.
#
# See also: 真, 假, 布林, 函式
# =============================================================================

# scalar/return-bool — return boolean literals
#
# GRAMMAR:
#   returnStmt :← '傳回' expr
#
# Verifies that the Swift backend emits functions returning
# boolean literals (SC-001: primitives, functions, return).

fn da_verum() → bool {
    return true
}

fn da_falsum() → bool {
    return false
}

radix/corpus/scalar/return-integer.fab (canonical · keyword)#

Function returning an integer literal via 傳回 42.

# =============================================================================
# 傳回 — Function returning an integer literal via 傳回 42.
# =============================================================================
#
# What this teaches:
#   • Integer return values — `傳回 42` returns an integer literal from a
#     `整數`-typed function
#   • Verifies the backend emits the correct primitive type for integer returns
#
# Common mistakes:
#   • Confusing a scalar (single-value) return with a collection return — this function returns one `整數`, not a list.
#
# See also: 空值, 函式
# =============================================================================

# scalar/return-integer — return integer literal
#
# GRAMMAR:
#   returnStmt :← '傳回' expr
#
# Verifies that the Swift backend emits a function returning an
# integer literal (SC-001: primitives, functions, return).

fn da_numerum() → int {
    return 42
}

radix/corpus/scalar/return-string.fab (canonical · keyword)#

Function returning a string literal via 傳回 \"文字\".

# =============================================================================
# 傳回 — Function returning a string literal via 傳回 "文字".
# =============================================================================
#
# What this teaches:
#   • String return values — `傳回 "文字"` returns a string literal from a
#     `文字`-typed function
#   • Verifies the backend handles string literal return types correctly
#
# Common mistakes:
#   • Confusing a scalar (single-value) return with a collection return — this function returns one `文字`, not a list.
#
# See also: 文字, 函式
# =============================================================================

# scalar/return-string — return string literal
#
# GRAMMAR:
#   returnStmt :← '傳回' expr
#
# Verifies that the Swift backend emits a function returning a
# string literal (SC-001: primitives, functions, return).

fn da_textum() → string {
    return "textus"
}