Functions — Return Values
Return values from functions effectively. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Return SumStudy Material
Read the full lesson
Getting data back from a function
In the previous lesson, our functions simply printed messages to the screen.
But what if you want a function to calculate a value, and then give that value back to you so you can use it in another part of your program?
To do this, you must use the return keyword.
pythondef double(number): result = number * 2 return result
When a function hits a return statement, it immediately stops running and spits that value back out to whatever called it.
pythonmy_number = double(5) print(my_number) # Output: 10
The double(5) call was completely replaced by its returned answer, 10, which was then stored in my_number.
The great trap: Print vs. Return
Understanding the difference between print() and return is the biggest hurdle for new programmers.
Think of it like being a chef in a restaurant.
When you use print(), you are walking out to the dining room and shouting the result to the customers. The program has no way to actually eat or use that food.
When you use return, you are putting the finished dish on the counter for the waiter to pick up and deliver. The rest of the program can now take that value and do whatever it wants with it.
If a function just prints a number, you cannot add 5 to it or save it to a database. The data is lost to the screen. If the function returns the number, you have full control over it.
As a general rule: if your function calculates something, it should almost always return the answer, not print it.
The invisible return value: None
What happens if you use a function in an expression, but that function doesn't actually have a return statement?
pythondef say_hi(): print(\"Hi!\") result = say_hi() print(result)
The output will be:
Hi! None
In Python, if a function finishes without returning anything, it automatically returns a special built-in value called None.
None literally represents the absence of a value. It is Python's way of saying: "There is nothing here."
Escaping early (Guard clauses)
Because a return statement immediately stops a function, you can use it to escape early if something goes wrong.
Imagine a function that divides two numbers. What if the user tries to divide by zero?
pythondef safe_divide(a, b): if b == 0: return \"Error: Cannot divide by zero!\" return a / b
The if b == 0 check is called a guard clause. It guards the rest of the function.
If b is zero, the function instantly returns the error message and stops. The a / b code never runs, so the program doesn't crash.
Guard clauses make your code much cleaner by handling errors right at the top, so the rest of your function can focus solely on the "happy path" logic without being wrapped in layers of if/else indentations.
What this lesson should give you
After this lesson, you should understand how to:
- get data out of a function using the
returnkeyword - deeply understand why
returnandprint()are completely different things - use returned values in variables and mathematical expressions
- recognize that functions without a return statement automatically yield
None - use early
returnstatements as guard clauses to protect your code from bad inputs
When you combine parameters (data coming in) with return values (data going out), you have mastered the core mechanics of functions!
Interactive
Exercises for this topic
These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.
Return Sum
Complete the "Return Sum" exercise.
Return Longer String
Complete the "Return Longer String" exercise.
Guard Clause
Complete the "Guard Clause" exercise.
Implicit None Return
Complete the "Implicit None Return" exercise.
Format Full Name
Complete the "Format Full Name" exercise.