Skip to content

返回

一句话解释

返回 语句 发送 值 后面 到 caller and 端 函数. 不使用 它, 函数 返回 无.

用大白话说

return statement sends a value back to whoever called the function and immediately ends the function's execution. The returned value can be used by the caller.

A function can return any type: a number, string, list, dict, or even another function. You can return multiple values as a tuple: 返回 , b — the caller can unpack them: x, y = func().

If a function ends without hitting a 返回 statement, or uses 返回 without a value, it returns 无. A function can have multiple return statements (e.g., in different branches of an if/else), but only one will execute.

示例

python
def add(a, b):
    return a + b

def min_max(numbers):
    return min(numbers), max(numbers)  # returns a tuple

def greet(name):
    print(f"Hello, {name}")
    # no return -> returns None

result = add(3, 5)
print(result)  # 8

lo, hi = min_max([4, 2, 8, 1])
print(lo, hi)  # 1 8

返回 发送 值 后面 and 端 函数. 多个 值 是 returned as tuple.

常见混淆

  • 容易混淆的对象: print

    关键区别: 返回 发送 值 后面 到 calling code for further use. print displays 文本 on screen 但 返回 无. returned 值 能 是 存储 in 变量; printed 值 cannot.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表