Concepts / Return Statements and Return Values

Return Statements and Return Values

When a return statement executes, the function exits immediately. No code after the return statement will run, even if it is on the same line or in the same block.

  • 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…

What a Return Statement Does

When you call a function, the program jumps into that function and executes its code line by line. But what happens when the function finishes? How does the program know to stop running the function's code and go back to where it was called? The answer is the return statement. The return statement does two things at once: it breaks out of the function (stops executing any more code inside it), and it sends a value back to the place where the function was called. Without a return statement, your function still exits when it reaches the end, but it sends back a special value called None, which means nothingness.

What do you think happens?

If you write a function that prints a message but never uses the return statement, what happens when you call it? Does the function keep running forever, or does it stop at some point?

  • The function keeps running forever until you stop the program

  • The function stops when it reaches the end and returns None

  • The function stops when it reaches the end and returns 0

  • The function never stops because there's no return statement

Reveal answer
Answer:

The function stops when it reaches the end and returns None

Every function in Python implicitly contains a return None statement at the end unless you have written your own return statement. This means that even if you don't explicitly write return, the function will still exit and send back None to the caller.

How Return Values Flow Back to the Caller

When a return statement executes, it does not just disappear. The value you return travels back through the call and becomes the result of that function call. Think of it like this: when you call a function, you are asking it a question. The return statement is the function's answer. That answer can then be stored in a variable, printed, used in a calculation, or passed to another function. The key insight is that a function call can be used anywhere you would use a value.

result = maximum(5,8)Enter functionmaximum()Compare 5 and 8return 8Value 8 travels backresult = 8call functionexecute codereach returnexit functionreceive value
What happens to the value you return? This diagram shows the journey of a returned value from inside the function back to the line of code that called it.

Return Stops Execution Immediately

One of the most important things to understand about the return statement is that it stops the function right away. Any code after the return statement inside that function will never run. This is called early exit. This is useful when you want to stop processing and send back a result without running the rest of the function. For example, if you are searching for a value in a list and you find it, you can return immediately without checking the rest of the list.

Start functionExecute line 1Execute line 2Condition true?return valueLine 3 SKIPPEDExecute line 3Exit functionyesnever reachedno
What happens to the code after a return statement? Does the rest of the function still run?
Key point

When a return statement executes, the function exits immediately. No code after the return statement will run, even if it is on the same line or in the same block.

Returning a Value Versus Returning Nothing

You can write a return statement in two ways. You can write return followed by a value, like return 42 or return name. This sends that value back to the caller. Or you can write return with nothing after it, like just return by itself. When you do this, the function still exits, but it returns None instead of a specific value. In fact, if you do not write any return statement at all, Python automatically adds an invisible return None at the very end of your function. This is why every function in Python always returns something, even if you did not explicitly write a return statement.

Function TypeReturn StatementWhat Gets ReturnedExample Use
Function that returns a valuereturn 42The number 42result = add(3, 4) stores 7 in result
Function that returns nothing explicitlyreturnNoneprint_message() runs but does not produce a value to store
Function with no return statement(implicit return None)Nonegreet() still returns None even though you did not write return

Comparison of different return behaviors

Worked Example: Finding the Maximum

Worked example

A Function That Returns the Larger of Two Numbers

Write a function called maximum that takes two numbers as parameters and returns the larger one. Trace through what happens when you call maximum(5, 8).

  1. 1
    Define the function with two parameters

    The function maximum takes two parameters, a and b. These will hold the two numbers we want to compare.

  2. 2
    Compare the two numbers with an if statement

    We check if a is greater than b. If it is, we know a is the larger number.

  3. 3
    Return the larger number

    If a is greater, we execute return a, which exits the function and sends the value of a back to the caller. The function stops here.

  4. 4
    Handle the else case

    If a is not greater than b, we reach the else block and execute return b, which sends the value of b back to the caller.

  5. 5
    The returned value becomes the result of the function call

    When we call maximum(5, 8), the function compares 5 and 8, finds that 8 is larger, and returns 8. This means the expression maximum(5, 8) evaluates to 8, so result = maximum(5, 8) stores 8 in the variable result.

Result

The function returns 8, and result holds the value 8.

python
Output (expected)
8
Notes on the code
  • Line 1-5: Define the function maximum with two parameters
  • Line 2-3: If a is greater than b, return a immediately (function exits here if true)
  • Line 4-5: Otherwise, return b (function exits here if the if condition was false)
  • Line 7: Call maximum(5, 8); the function compares 5 and 8, finds 8 is larger, and returns 8
  • Line 8: Print the result, which is 8

Common Mistakes with Return Statements

  • Common mistake

    Forgetting to actually use the returned value

    What it looks like

    maximum(5, 8) is called but the result is not stored or printed, so you never see the answer

    Why it fails

    The function returns 8, but if you do not store it in a variable or print it, the value is lost and you cannot use it

    Do this instead

    Store the result in a variable (result = maximum(5, 8)) or print it directly (print(maximum(5, 8)))

  • Common mistake

    Writing code after a return statement inside the function

    What it looks like
    return a followed by print(a) on the next line inside the function
    Why it fails

    The return statement exits the function immediately, so the print statement will never run

    Do this instead

    Put any code that needs to run before the return statement, or remove the code after return if it is not needed

  • Common mistake

    Confusing return with print

    What it looks like

    Writing print(a) instead of return a, thinking they do the same thing

    Why it fails

    print() displays a value on the screen but does not send it back to the caller. The function still returns None

    Do this instead

    Use return to send a value back to the caller; use print only if you want to display something on the screen

  • Common mistake

    Assuming a function without an explicit return statement returns 0

    What it looks like

    Calling a function that has no return statement and expecting it to give you 0

    Why it fails

    Functions without an explicit return statement return None, not 0

    Do this instead

    Remember that every function implicitly returns None unless you write your own return statement

Multiple Return Statements in One Function

A function can have more than one return statement. This is common when you have different conditions and want to return different values depending on which condition is true. When the function runs, it will execute the code line by line until it hits a return statement. At that moment, it exits and sends back the value from that specific return statement. The other return statements are never reached. This is why it is safe to have multiple return statements in different branches of an if/else structure.

python
Output (expected)
B
Notes on the code
  • Line 1: Define function with one parameter, score
  • Line 2-3: If score is 90 or higher, return A and exit the function
  • Line 4-5: If score is 80 or higher (but less than 90), return B and exit
  • Line 6-7: If score is 70 or higher (but less than 80), return C and exit
  • Line 8-9: Otherwise, return F and exit
  • Line 11: Call check_grade(85); since 85 is >= 80 but < 90, the second condition is true and B is returned
  • Line 12: Print B
Key point

Only one return statement will ever execute in a single function call. Once a return statement runs, the function exits immediately, and none of the other return statements are checked or executed.

Practice: Trace the Execution

MEDIUM

Read this function and predict what will be printed. Then trace through the execution step by step.

def mystery(x):
    if x < 0:
        return "negative"
    if x == 0:
        return "zero"
    return "positive"
result = mystery(-5)
print(result)

What does this function return when called with -5? Which return statement executes? Why do the other return statements not run?

Hints
  • Start at the top of the function and trace line by line
  • When you reach a return statement, the function exits immediately
  • Check which condition is true for x = -5
EASY

Write a function called is_even that takes one number as a parameter and returns True if the number is even, or False if it is odd. Your function should use a return statement to send back the boolean value. Test your function by calling it with a few different numbers.

Hints
  • A number is even if it divides evenly by 2 (use the modulo operator %)
  • You need two return statements, one for the even case and one for the odd case
  • Remember to actually call your function and print or store the result

Key Takeaways

  1. 1
    The return statement exits a function immediately and sends a value back to the caller. Any code after the return statement does not run.
  2. 2
    A function can return a specific value (like return 42) or return nothing explicitly (just return), which sends back None.
  3. 3
    Every function in Python implicitly returns None at the end if you do not write your own return statement.
  4. 4
    The value returned by a function becomes the result of the function call and can be stored in a variable, printed, or used in calculations.
  5. 5
    A function can have multiple return statements in different branches (if/else blocks). Only one will execute per function call, and it exits the function immediately.

At a glance

  • The return statement breaks out of a function and sends a value back to the caller
  • Functions always return something: either an explicit value you specify, or None if no return statement is present
  • When a return statement executes, the function exits immediately and no code after it runs
  • The returned value becomes the result of the function call and can be used wherever a value is expected
  • Multiple return statements in different branches are safe because only one will execute per function call

Related concepts

More concepts starting with R