Return values and composition
Printing is for people reading output. Returning is how one part of a program provides a value to another. This distinction turns a one-off demonstration into a useful building block.
Printing and returning are not interchangeable
print(result) sends text to an output stream; it does not make that text the function result. return result ends the call and hands a Python value back to the caller. You can then store, compare or pass that value to other functions.
If a function reaches its end without a return, Python returns None. That is a real value meaning the function did not explicitly provide a result; it often surprises beginners who expected printed text to become a number.
Build on results instead of copying calculations
An output such as a discounted price can feed a second function that adds a delivery charge. Each function has one clear job. A later interface, web tool or application can call the same calculation code.
Keep presentation at the edges: calculate the numeric result in the helper and format it for display where it is used. This reduces duplicated rounding or formatting logic.
Use early returns for clear rules
return stops execution of the current function immediately. It can handle exceptional but expected cases before the main calculation, rather than burying the happy path inside several nested if statements.
A guard may return a special value such as None for missing data, but that makes the result more complex for callers. In a later lesson we will learn how to reject invalid inputs explicitly with exceptions.
A predictable helper is easy to test
A function that calculates a result from its arguments without relying on hidden external state is often called a pure function. The same valid inputs give the same output, which makes examples and unit tests straightforward.
Not every real function can be pure — reading a file or checking the current time is inherently contextual. Still, isolating the predictable calculation from external effects makes future tools easier to trust.
Try it yourself.
Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.
The returned value can be stored and used again.
Press Run Python to execute the editable code.
Change the argument to 11. Which printed values change?
Need a hint?
The function returns an integer; the caller decides what to do next.
On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.
Check your understanding.
Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.
Why is the return value of a function that only calls
print()usuallyNone?View answer
print()performs the side effect of displaying text; it does not automatically return that text as the containing function’s value. A function with no explicitreturnstatement finishes with the implicit valueNone. For instance,def show(): print(5)prints 5 butresult = show()assignsNonetoresult.What is an advantage of returning a number from a helper instead of printing a formatted sentence inside it?
View answer
A numeric return value remains usable by other code: a caller can compare it, add it to another amount, place it in a dictionary or format it for a particular interface. If the helper prints only a sentence, the numerical result is not readily available for follow-on calculations. Separating calculation from presentation also makes the helper easier to test and reuse.
Where this leads.
To write dependable functions, we also need to understand where names exist and when they stop referring to local data.