Understand the idea
A problem you can recognise
Your total calculator works, and now two parts of a program need the same calculation. Copying the code would create two places to fix later. A function lets you name the operation and ask for its result, but a printed message is not necessarily that result.
The puzzle to keep in mind is simple: a function can visibly print 150 while its caller receives None. We will separate those two channels carefully.
Understand the idea
Understand the idea
A function gives a reusable operation a name. Its definition describes parameters and a body; calling it supplies arguments and executes that body. Merely defining a function does not run its calculation. Keep the purpose small enough that you can explain the input and output contract in a sentence.
return sends a value back to the caller and exits the current function call. print writes a display side effect; it is not a replacement for returning a value that another calculation needs. A function without an explicit returned value returns None. This distinction matters when you want to combine operations instead of displaying everything immediately.
Local names created during a call are separate from the caller names that supplied values. Avoid relying on unrelated global variables when an argument would make the dependency explicit. Functions are easier to test when the same explicit input produces the same result without changing outside state. Later lessons introduce more parameter rules and scope behaviour, but a clear calculation and return is the best starting point.
See how it works
Example 1: Return a calculated result
PYTHON exampleRead the code, then try a change.
Try this example
def study_total(days, minutes):
return days * minutes
result = study_total(5, 30)
print(result)
Expected output:
ReferenceCompare this with the explanation.
150
The function receives two arguments, computes their product and returns it. The caller chooses to print the returned result. No output occurs merely from defining study_total.
See how it works
Example 2: Recognise an implicit None
PYTHON exampleRead the code, then try a change.
Try this example
def show_message():
print("Ready")
result = show_message()
print(result)
Expected output:
ReferenceCompare this with the explanation.
Ready
None
The function displays Ready but has no explicit return expression. The caller therefore receives None, which the final print makes visible.
See how it works
Example 3: Compose returned values
PYTHON exampleRead the code, then try a change.
Try this example
def double(value):
return value * 2
print(double(10) + double(5))
Expected output:
ReferenceCompare this with the explanation.
30
Each call returns a number. The addition combines those numbers before the outer print displays the total. This would not work the same way if double only printed its result.
Understand the idea
Think through the important decisions
At a call, work out the argument values first. Follow them into the function's parameters, execute the function body, and identify the value that return sends back. Then continue the caller's expression with that returned value. Drawing this path prevents a common mistake: treating the function definition itself as if it had already performed the calculation.
Printing is an observable action. Returning supplies a value to the caller. A reusable total function can return a number so another caller can add it, compare it or format it. If it only prints, the caller cannot use the displayed characters as though they were the numeric return value.
Look at every possible path through a function. A path that reaches the end without an explicit return produces None. A conditional return in one branch does not automatically supply the same kind of value on every other branch. Choose an input for each path and predict what the caller receives.
Check your understanding
Check the reasoning
return supplies a value to the caller; print only displays a value.
A matching screenshot or output is one check, not proof that a program is correct for all inputs. Change one input, predict the new result and run again. If the result differs, find the first instruction where your prediction stops matching the program state. Keep that small case as a regression check when you fix the cause.
Spot the mistake
The mistake worth understanding
Returning ends that function call. Statements after an unconditional return on the same execution path do not run. A display statement before the return can still run, but mixing calculation and presentation makes reuse and testing harder unless the function's purpose genuinely includes both.
Your turn
Independent practice
Define a function taking daily minutes and number of days, return the total, and call it with two different inputs. Keep printing outside the function. Explain how you would test the return value without inspecting screen output.
Your turn
Practice hint
Write return inside the function. Store or compare the call result before printing it.
Change it and explain it
Experiment with the first program Define a function taking daily minutes and number of days, return the total, and call it with two different inputs. Keep printing outside the function. Explain how you would test the return value without inspecting screen output. Start by changing one input in this smaller example. Loading it does not execute it.
def study_total(days, minutes):
return days * minutes
result = study_total(5, 30)
print(result) Open in Practice Loading an example does not run it. Your existing edits are protected by a replacement confirmation.
Your turn
Try a different case
Call the same total function with two input pairs and store the two results under different names. Demonstrate that you can inspect or combine the results without editing the function. Explain why this is different from copying its printed output.