Course contentsShow
← Computer Science
Lesson 199 of 2,8725. Functions and ModularityFree lesson

Return Statements and Values

Understand how return statements work, how they exit functions, and how they pass values back to callers.

Return Statements and Values

You'll learn how functions send data back to the code that called them.

What is a Return Statement?

Think of a function like a helpful assistant you send on an errand. You ask them to calculate something or fetch information. The return statement is how they report back to you with an answer.

When a function executes a return statement, two things happen:

  1. The function immediately exits (stops running, even if there's more code below)
  2. It sends a value back to whoever called it

How Return Works

def add_numbers(a, b):
    result = a + b
    return result  # Send the sum back to the caller
    print("This never runs!")  # Unreachable after return

answer = add_numbers(5, 3)  # answer now holds 8

The return keyword is followed by the value you want to send back. This value can be:

  • A literal number or string
  • A variable
  • The result of an expression
  • Even another function call

Using Returned Values

The caller receives whatever the function returns and can use it:

total = add_numbers(10, 20)  # Store it in a variable
print(add_numbers(7, 2))      # Use it directly
if add_numbers(3, 4) > 5:     # Use in a condition
    print("Greater than 5")

Without a return statement (or with return alone), functions return a special "nothing" value.

Key Takeaway: Return statements exit functions immediately and pass values back to callers, allowing functions to compute results that other parts of your program can use.