Return Statements and Outputs
Overview
Functions can send data back to the part of the program that called them. This lesson covers how return statements work, how to handle multiple return values, and how returned data can be used in other expressions to build more complex program logic.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What return does: Understand how functions send values back to the caller.
- Using return values: Learn how to use the value a function returns.
- Return vs print: Distinguish between returning a value and printing it.
- Functions without return: Understand what happens when a function doesn't return anything.
Why This Matters
Return statements let functions produce results that you can use elsewhere in your program. Instead of just printing something (which you can't use later), returning a value lets you store it, use it in calculations, or pass it to other functions. This makes functions much more powerful and flexible.
Step 1: What Is a Return Statement?
A return statement sends a value back from a function to wherever it was called. When Python encounters a return statement, it immediately exits the function and sends the value back. Think of it like a function answering a question - you ask it to do something, and it gives you back the answer.
def add(a, b):
result = a + b
return result
sum = add(3, 5)
print(sum) # Output: 8
Calculate the Result
Inside the function, you perform calculations or operations. In this example, result = a + b adds the two numbers together.
Return the Value
Use the return keyword followed by the value you want to send back. When Python sees return result, it immediately exits the function and sends the value of result back to where the function was called.
Use the Returned Value
When you call the function, you can store its return value in a variable. In sum = add(3, 5), the function returns 8, which gets stored in the variable sum. You can then use sum anywhere in your program.
Key Concept: A return statement immediately exits the function and sends a value back. Any code after a return statement in the same function won't run because Python leaves the function as soon as it hits return. The returned value can be stored in a variable, used in expressions, or passed to other functions.
Mini Practice #1: Your First Return Statement
Try It YourselfTry calling the function and storing its return value. Notice how you can use the returned value:
What happened? When Python calls multiply(4, 5), the function calculates 4 * 5 = 20 and returns that value. The return value (20) gets stored in the variable product. Then you can use product just like any other variable - you can print it, use it in calculations (like product * 2), or pass it to other functions. This is the power of return statements: they let functions produce values that you can use throughout your program!
Step 2: Return vs Print
It's important to understand the difference between return and print. They serve different purposes:
Print (Display Only)
Shows output to the user but doesn't give you a value you can use.
def add_print(a, b):
print(a + b)
result = add_print(3, 5)
# result is None!
# You can't use the value
Prints to screen but returns None
Return (Use the Value)
Gives you a value you can store, use in calculations, or pass to other functions.
def add_return(a, b):
return a + b
result = add_return(3, 5)
# result is 8!
# You can use it anywhere
Returns a value you can use
When to Use Each
Use print when you just want to display something to the user. Use return when you need the function to produce a value that you'll use elsewhere in your program. If a function doesn't have a return statement, it automatically returns None (which means "nothing").
Step 3: Using Return Values
You can use return values in many ways - store them in variables, use them directly in expressions, or pass them to other functions:
def calculate_area(length, width):
return length * width
# Store in a variable
area = calculate_area(5, 3)
print(area)
# Use directly in an expression
total = calculate_area(5, 3) * 2
print(total)
# Use in another function call
print("Area is", calculate_area(5, 3))
Remember: When you call a function that returns a value, you can use that value anywhere you would use a variable or a literal value. The function call (like calculate_area(5, 3)) literally becomes the value it returns (like 15).
Mini Practice #2: Using Return Values
Try It YourselfTry using return values in different ways. Notice how flexible they are:
What happened? The function square returns the square of a number. You can store its return value in a variable (result1 = square(5)), use multiple return values in an expression (result2 = square(3) + square(4)), or use the return value directly in a print statement (print("Square of 10:", square(10))). Each time you call square(), it returns a value that you can use immediately without storing it first. This flexibility makes return statements very powerful!
Step 4: Functions Without Return
If a function doesn't have a return statement, it automatically returns None. This is useful for functions that do something (like printing) but don't need to produce a value:
def greet(name):
print("Hello, " + name + "!")
result = greet("Alice")
print(result) # Output: None
Remember: Functions that don't explicitly return a value automatically return None. This is Python's way of saying "this function doesn't produce a value." If you try to use None in calculations or operations that expect a number, you'll get an error.
Step 5: Return Ends the Function
When Python encounters a return statement, it immediately exits the function. Any code after the return statement in that function won't run:
def check_age(age):
if age >= 18:
return "Adult"
print("This won't print") # Never runs!
return "Minor"
print(check_age(20)) # Output: Adult
How It Works
As soon as Python sees return "Adult", it exits the function immediately. The print statement after it never runs because Python already left the function. This is useful for early exits - you can check conditions and return early if needed, without running the rest of the function.
End-of-Lesson Exercises
Exercise 1: Create a Function That Returns a Value
Create a function named calculate_perimeter that takes two parameters: length and width. It should return the perimeter (2 × length + 2 × width). Store the result in a variable and print it.
Use return to send back the calculated perimeter value.
Exercise 2: Use Return Value in Expression
Create a function named double that takes one parameter number and returns number * 2. Then use it in an expression: calculate double(5) + double(3) and print the result.
Your function should return the doubled value, then use it in an expression.