Concepts / Return Values and the Return Statement

Return Values and the Return Statement

Think of return as a control-flow action and a value-transfer action. It tells the function to stop, then tells the caller what result came back, if a result was provided.

  • Programming
Interactive lab

Try it: Defining and Calling Functions

What def and a call really do: def only creates a function object, a call binds the arguments to the parameters in a new frame and runs the body, return ends the call at once and its value replaces the call expression in the caller — while a function that only prints gives back None.

How it works

  1. def name(params): creates a function object and binds the name to it; the body does not run yet.
  2. A call evaluates its arguments, opens a new frame, binds each parameter to its argument and runs the body from the top.
  3. return value ends the call immediately (lines after it never run) and sends the value back; the frame and its local names are discarded.
  4. In the caller, the call expression is replaced by the returned value, which can be stored, used in a larger expression or passed to another call.
  5. A body that ends without return (e.g. one that only prints) makes the call evaluate to None — so x = f() stores None and None + 1 raises TypeError.
  6. Calling a name before its def has run raises NameError: the function does not exist yet.

Default run (18 steps): The program is ready. Python runs it from the top, one statement at a time. Idea: the value a call returns is what the caller stores. … The program finished. Console: 8.

Simplified: Four fixed, hand-written programs; you edit the literal arguments, switch the body between return and print, and move the def after the calls. The lab's own small evaluator runs them (your input is never executed as code); every variant was run in CPython 3.12 and the lab reproduces its sys.settrace line/call/return/exception events, its output and its error message. Functions are shown as <function name> without a memory address.

Educational simulation

Loading the simulation…

The Function Call Has a Destination

A function call does not have to be the end of a computation. A function can perform work, produce a result, and send that result back to the code that called it. The return statement performs both parts of this job: it ends the current function execution and optionally passes a value back to the caller.

Key point

Think of return as a control-flow action and a value-transfer action. It tells the function to stop, then tells the caller what result came back, if a result was provided.

Callercalls functionFunction bodyexecutes in orderreturnfunction stopsCaller continuesreceives resultcallexecution reachescontrol passes back
What happens to execution when a return statement is encountered, and where does control go next?

Tracing the Stop Point

When a function is called, Python enters that function and executes its statements in order. The moment Python encounters return, it stops executing the rest of that function body. Control immediately passes back to the line that called the function. Any statements written after that return in the same function do not execute during that call.

Worked example

Following a return through a function

A function performs two steps, reaches a return statement, and has another statement after return. Which parts execute?

  1. 1
    Enter

    The call transfers execution into the function, and Python begins with the first statement.

  2. 2
    Execute in order

    Statements before return execute in their normal order.

  3. 3
    Reach return

    The return statement is encountered, so the function stops immediately.

  4. 4
    Skip the remainder

    Statements after return in that function body do not execute for this call.

  5. 5
    Resume at the caller

    Control goes back to the code that called the function.

Result

Only statements before and including the return action take part in this call; the caller resumes after the function call.

Callerfunction callFirst statementexecutesreturnstops functionLater statementskippedCaller resumesafter callenter functionnextdoes not continuecontrol returns
Which statements run before and after return, and which statements are skipped?

What do you think happens?

A function reaches return and then has two more statements in its body. What happens to those two statements during that call?

  • They execute before control returns

  • They execute after control returns

  • They do not execute for that call

Reveal answer
Answer:

They do not execute for that call.

Once return is reached, no further statements in that function execute. Control immediately passes back to the caller.

The Value Crossing the Boundary

Definition

A return value is the value sent from a function back to the code that called it. The value after return, if one is present, becomes the result of that function call.

The caller can use a returned value in several ways. It can assign the value to a variable, pass it to another function, print it, or use it as part of an expression. This makes return the bridge between a function's internal computation and the rest of the program.

In the real world

Imagine a function that takes two numbers and returns the larger one. The comparison happens inside the function. The larger number then crosses back to the caller, where the caller can store it, print it, pass it onward, or use it in another expression.

Two numbersfunction inputFunction computationfind larger valueLarger numberreturn valueCaller usestore, print, pass, orcombineprovided toproducessent back
How does a value produced inside a function move back to the code that called it?
Key point

The function does not keep the returned value isolated from the rest of the program. Once returned, the caller receives it as the result of the function call and decides how to use it.

Choosing Which Return Runs

A function can contain multiple return statements. However, only one return executes during any single call. Which one runs depends on which condition is met during that call. As soon as one return is reached, the function exits, so the other return statements are not reached for that call.

Worked example

A function with alternative results

A function compares two numbers and has one return for the larger first number and another return for the larger second number. What happens in one call?

  1. 1
    Receive inputs

    The function begins with the two numbers supplied by its caller.

  2. 2
    Evaluate the condition

    The function determines which comparison result applies to those inputs.

  3. 3
    Reach one return

    The return associated with the met condition sends the larger number back.

  4. 4
    Stop immediately

    The function exits at that return, so the alternative return is not executed during the same call.

Result

One call produces one returned result through one executed return statement.

Function withreturn valuecaller receives returnedvalueFunction withoutexplicit valueno value stated afterreturnReturned valueresult of callNonedefault resultreturnsreturns by default
What does the caller receive from a function with a return value compared with a function that has no explicit return value?

Mistakes Beginners Make

  • Common mistake

    Assuming statements after return still execute

    What it looks like

    A function reaches return and is expected to perform another action later in its body.

    Why it fails

    Once return is reached, no further statements in that function execute for that call.

    Do this instead

    Trace the function in order and mark return as the point where execution leaves the function.

  • Common mistake

    Treating return as only a way to stop

    What it looks like

    The caller expects a computation from the function, but the returned value is ignored conceptually.

    Why it fails

    Return can both exit the function and send a value back to the caller.

    Do this instead

    Ask two questions: where does control go next, and what value, if any, reaches the caller?

  • Common mistake

    Expecting every function to return a meaningful value

    What it looks like

    A function has no explicit returned value, but the caller is assumed to receive a computed result.

    Why it fails

    Functions that do not explicitly return a value return None by default.

    Do this instead

    Check whether a return statement supplies a value before deciding what the caller receives.

  • Common mistake

    Thinking every return statement runs

    What it looks like

    A function has multiple possible returns, and all of them are treated as part of one call.

    Why it fails

    Only one return executes per call, depending on which condition is met.

    Do this instead

    Identify the first return reached along the execution path and stop tracing that function there.

Practice the Execution Trace

MEDIUM

Describe the execution flow for a function that receives two numbers, compares them, and returns the larger one. Include what happens inside the function, when execution stops, where control goes, and how the caller can use the returned number.

Hints
  • Start at the function call and move through the function's statements in order.
  • Identify the condition that determines which return statement is reached.
  • State what happens to statements after the selected return.
  • Finish by naming at least two ways the caller can use the returned value.
Worked example

Practice answer

Trace a call to a function that compares two numbers and returns the larger one.

  1. 1
    Call

    The caller invokes the function with two numbers, so execution enters the function.

  2. 2
    Compare

    The function evaluates which number is larger.

  3. 3
    Return

    The return statement associated with the met condition sends the larger number back.

  4. 4
    Exit

    The function stops immediately at return; no later statement in that function executes for this call.

  5. 5
    Use

    The caller can assign, pass, print, or use the returned number in an expression.

Result

The larger number becomes the result of the function call and is available to the caller.

A Reliable Mental Model

  1. 1
    Use this four-part trace whenever you read a function with return: enter the function, execute statements in order, stop immediately when return is reached, and resume at the caller with the returned value if one was supplied. If no value is explicitly returned, the caller receives None by default.
  • Return exits the current function.
  • Statements after the reached return do not execute during that call.
  • A value after return becomes the result of the function call.
  • The caller can use a returned value in several ways.
  • Only one return executes per call, and a function without an explicit returned value returns None by default.

Key Takeaways

  • The return statement immediately exits a function and transfers control back to its caller.
  • A value written after return is sent back as the result of the function call.
  • The caller can assign, pass, print, or use the returned value in an expression.
  • Only one return executes during a single call, even when a function contains multiple return statements.
  • A function that does not explicitly return a value returns None by default.

Related concepts

More concepts starting with R