渲染zh-Hans

返回

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans 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"
}