Send a useful result back to the code that called the function.
A function can receive input, do work, and send a result back. This completes the basic flow: input → function → returned value. The calling code can then decide what to do with that value.
def double(number):
return number * 2
result = double(4)In double(4), 4 is the argument. During this call, the parameter number receives 4. The body calculates 4 * 2, and return sends 8 back to the caller. The call double(4) therefore gives the value 8.
Read result = double(4) in two small steps: double(4) → 8, then result = 8. A function call that returns a value can be treated as that value in the surrounding code.
Follow the value through the function: argument → parameter → function body → return value → caller. An argument gives the function input; return sends its result back to the place where the call happened.
def calculate_total(price, quantity):
return price * quantity
total = calculate_total(250, 3)
print(total)At calculate_total(250, 3), price = 250 and quantity = 3. The body calculates 250 * 3, which is 750. return 750 sends that value back, so total = 750. Finally, print(total) displays 750.
def show_total(price, quantity):
print(price * quantity)
def calculate_total(price, quantity):
return price * quantityprint() displays a value for a person to see. That is useful, but it does not by itself give the calculated value back to the rest of the program. return sends the value to the caller, which can save it, print it, or use it in another calculation.
result = double(4)
print(result)
print(double(4))
next_result = double(4) + 2The same returned value can be stored, displayed directly, or used in a later simple expression. Here double(4) gives 8, so next_result receives 10.
def example():
print("Before")
return 10
print("After")Python prints Before, then return 10 sends back 10 and ends this call to example(). The later print("After") does not run during that call.
answer = double(5) + 1
# double(5) → 10
# answer = 10 + 1
# answer = 11First replace the call mentally with the value it returns. Then continue reading the caller. This makes function calls easier to trace without treating them as mysterious special syntax.
A practical return-reading checklist
- Find the callWhich function is called, and what arguments does this call provide?
- Map arguments to parametersDecide which values the parameters represent during this call.
- Trace the bodyFollow the calculation or other instructions in the function body.
- Find the returned valueWhat expression or value does
returnsend back? - Replace the call mentallyReplace the call with its returned value, then keep reading the caller.
Common beginner mistakes
- Confusing print() with return
print()displays a value;returnsends a value back to the caller for later program logic. - Calculating a value but forgetting to return itIf later code needs the result, the function must return that result instead of only calculating or displaying it.
- Expecting code after return to runOnce Python executes
returnfor this call, the function call ends and later body lines are not reached. - Ignoring a result that is needed laterCalling a function can do its calculation, but save the returned value in a variable when the caller needs to use it later.
- Thinking return permanently changes an argument
returnsends a result back from the current call. It does not permanently replace the value that was passed in as an argument. - Not asking where the returned value goesLook at the call site: is the result assigned to a variable, printed, or used in another expression?
