A return value is the object a function produces and sends back to its caller using the return statement. When Python encounters return expr, it evaluates the expression, passes the resulting object back, and immediately exits the function. If a function reaches the end of its body without a return statement, or executes a bare return with no expression, it implicitly returns None.
Python functions can return multiple values by returning a tuple: return x, y, z. The caller can unpack the result with tuple unpacking: a, b, c = f(). This idiom is extremely common — the built-in divmod() returns both quotient and remainder, and dict.items() yields key–value tuples. Named tuples or dataclasses can make multi-value returns more self-documenting.
The return statement is also used for early exit. A function might check preconditions at the top and return immediately if they are not met (a "guard clause"), avoiding deeply nested if blocks. Generators use yield instead of return to produce a sequence of values lazily, pausing and resuming execution between yields.
Discussed in:
- Chapter 5: Functions — Return Values