Understanding Function Return Values
Void functions perform actions but return None, not a usable value.
The Hidden Result of an Action
A function call can have two separate effects: it can perform an action, and it can return a value. These are not the same thing. A function may print text or update something visible while still returning no usable result. When that happens, Python represents the absence of a returned value with the special value None.
What do you think happens?
What will result contain after this call?
Reveal answer
Answer: None
The function performs its printing action, but it does not return a value. The action appears on the screen, while the function result assigned to result is None.
Bing
Bing
NoneVoid and Fruitful Functions
A void function performs an action for its side effect, such as printing text or writing to a file, but returns None rather than a usable value. A fruitful function computes and returns meaningful data. That data flows back to the place where the function was called and can be stored, calculated with, or passed to another function.
| Function kind | Main purpose | Result for the caller | Typical next use |
|---|---|---|---|
| Void function | Perform an action | None | Use the side effect |
| Fruitful function | Compute a value | A meaningful value | Assign it, calculate with it, or pass it onward |
Where Returned Values Go
A fruitful function sends its result back to the exact place where the call appeared. If the call is on the right side of an assignment, the result is stored in a variable. If the call is part of an expression, the result participates in that calculation. If the call is an argument to another function, the returned value is passed into that function.
3
1.618033988749895Do not assign a fruitful function's result automatically. Decide based on what you need: assign it when you will refer to it later, use it directly in an expression when you need it once, or pass it directly to another function when that is the next operation.
None as a Real Python Value
None is a special Python value with its own type, called NoneType. It represents the absence of a returned value from a void function. None is different from the string 'None': the string is text, while None is the special value Python uses when no value was returned.
| Expression | What it represents | What appears when displayed |
|---|---|---|
| None | Python's special no-return value | None |
| 'None' | A string containing the text None | None |
Interactive Mode and Scripts
In interactive mode, Python automatically displays the return value of an expression that you type. For example, entering a fruitful call such as math.sqrt(5) shows its result immediately. In a saved script, the same call computes the result silently unless you explicitly print it or assign it to a variable.
| Context | Call | What happens to the returned value |
|---|---|---|
| Interactive mode | math.sqrt(5) | Python displays the result automatically |
| Script | math.sqrt(5) | The result is computed but not displayed unless code prints or assigns it |
Finding an Unexpected None
An unexpected None usually means that the function was used as though it were fruitful even though it was void. Start debugging by asking what the function is supposed to provide. Then inspect the function call: does it perform an action only, or does it return meaningful data? If the variable receiving the result contains None, the function did not provide a usable value for that call.
Assuming that visible output is the function's return value.
The text appears on the screen because the function performs its action. The function result is still None.
Fix:
Separate the action you observe from the value returned to the caller.Using a void function result in a calculation.
If result came from a void function, it contains None, which cannot be used in a meaningful calculation.
Fix:
Use a fruitful function that returns the required value, or use the void function only for its side effect.Treating None as the string 'None'.
None is Python's special value with type NoneType, while 'None' is text.
Fix:
Keep the special value and the text string conceptually separate.
Suppose a variable unexpectedly contains None after a function call. Trace the value backward to the call that produced it. If that function printed something, changed a display, or performed another action but did not return meaningful data, the None came from that function. The repair is not to treat None as a number; the repair is to choose or write a function that returns the value the rest of the code needs.
Practice the Value Journey
For each call, identify whether the result should be captured, used directly in an expression, passed to another function, or ignored because the function is being used for its action. Then explain what value reaches the next step.
Hints
- Ask whether the function computes meaningful data or performs an action.
- A fruitful result can be assigned, calculated with, or passed onward.
- A void function's result is None.
Choosing What to Do with a Return Value
A function returns a meaningful value, and the code needs that value in two later calculations. Should the call be assigned to a variable or left as a standalone call?
Identify the function kind: Because the function returns meaningful data, it is fruitful.
Count the future uses: The result is needed more than once, so giving it a meaningful variable name makes it available for later references.
Use the stored result: The variable can participate in each later expression instead of calling the function again or losing the returned value.
Assign the fruitful function's result to a variable when you need to refer to that result multiple times.
Key Takeaways
- Void functions perform actions for their side effects and return None.
- Fruitful functions return meaningful values that flow back to the caller.
- None is a real Python value of type NoneType, not the string 'None'.
- A returned value can be assigned, used in an expression, or passed to another function.
- Interactive mode displays expression results automatically, while scripts require explicit use of the result.
Key Takeaways
- A function's visible action and its returned value are separate.
- A void function returns None, even when it prints or performs another noticeable action.
- A fruitful function returns data that can be captured or used immediately.
- Unexpected None is a debugging signal that code may be using a void function where a fruitful one is needed.
- Interactive mode displays returned expression values automatically; scripts do not.