Lesson 6 of 12
Structured learning draftWrite a function that returns a useful value
Separate a stock decision from the way it is displayed. A small function lets different parts of a program use the same rule and makes it possible to test the decision without capturing text printed to a terminal.
Learning objectives
- Pass values into a function.
- Return a Boolean result.
- Separate calculation from presentation.
Write a function that returns a useful value: the working sequence
Trace the three stages of write a function that returns a useful value and compare them with the example.
The caller supplies a quantity and threshold.
The function evaluates a single reusable comparison.
The caller receives True or False and chooses what to do next.
A function receives arguments and can return a result to its caller. Printing is a side effect; returning a Boolean lets the caller decide whether to display an alert, build a report or run a test.
def needs_reorder(quantity, threshold):
return quantity <= threshold
print(needs_reorder(5, 5))
print(needs_reorder(6, 5))True
FalseTry it, then change it
- Call the function for quantities 4, 5 and 6.
- Change the threshold while keeping quantity fixed.
- Replace return with print temporarily and inspect what the caller receives; then restore return.
Common mistakes
- Printing when the caller needs a return value.
- Depending on hidden global values instead of explicit arguments.
- Forgetting to call the function and treating its name as its result.
Practice activity
Apply the lesson
Write remaining(opening, received, sold) and use its returned value in two different messages. Test a normal calculation and a zero balance. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
A function reaches its end without executing return. What does its caller receive?
Lesson summary
- Return values allow a rule to be reused.
- Explicit arguments make a function easier to test.
- Keep the decision separate from the display.
Sources and further reading
- Defining functionsPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.