Concepts / Return Values and the Return Statement

Return Values and the Return Statement

The return statement exits a function and optionally sends a value back to the code that called it.

  • Programming

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.

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.

callexecution reachescontrol passes backCallercalls functionFunction bodyexecutes in orderreturnfunction stopsCaller continuesreceives result
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.

Following a return through a function

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

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

Execute in order: Statements before return execute in their normal order.

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

Skip the remainder: Statements after return in that function body do not execute for this call.

Resume at the caller: Control goes back to the code that called the function.

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

enter functionnextdoes not continuecontrol returnsCallerfunction callFirst statementexecutesreturnstops functionLater statementskippedCaller resumesafter call
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

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.

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.

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

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.

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?

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

Evaluate the condition: The function determines which comparison result applies to those inputs.

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

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

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

returnsreturns by defaultFunction withreturn valuecaller receives returnedvalueFunction withoutexplicit valueno value stated afterreturnReturned valueresult of callNonedefault result
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

  • Assuming statements after return still execute

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

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

  • Treating return as only a way to stop

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

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

  • Expecting every function to return a meaningful value

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

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

  • Thinking every return statement runs

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

    Fix: 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.

Practice answer

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

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

Compare: The function evaluates which number is larger.

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

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

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

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

A Reliable Mental Model

  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.