Using Built-in Functions
A fruitful function call produces real data. The data can be a number, a string, a list, or another Python object. It does not automatically disappear; it goes back to the exact place where the function was called.
Following the Returned Value
A built-in function can do more than perform an operation. It can produce a value and send that value back to the code that called it. For example, a mathematical function can calculate a number, and your code can store that number, include it in another calculation, or pass it to another function. The important question is not only whether the function runs, but also what your code does with the value that comes back.
A fruitful function call produces real data. The data can be a number, a string, a list, or another Python object. It does not automatically disappear; it goes back to the exact place where the function was called.
Fruitful and Void Functions
A fruitful function returns a value to its caller. That returned value can be captured in a variable or used as part of an expression. A void function performs an action but returns no useful value. Its purpose is its side effect, such as printing text or writing to a file, rather than producing a result for another calculation.
| Function kind | What it does | What comes back | Typical reason to call it |
|---|---|---|---|
| Fruitful | Performs a computation | A value | To use the result in later code |
| Void | Performs an action | No value; assigning its result gives None | To produce a side effect |
The distinction is whether a usable value returns to the caller.
Capturing a Function Result
Using Two Mathematical Results
How can the results of fruitful built-in function calls be used in later code?
- 1Store a direct result
The call
math.cos(0)computes the cosine of 0 radians and returns 1.0. Assigning the call to x stores that returned value in x. - 2Use a result immediately
The call
math.sqrt(5)returns approximately 2.236. That value can be used directly inside a larger arithmetic expression instead of being assigned first. - 3Keep the purpose in view
In both cases, the function's result is captured or used. The returned value does not disappear.
A fruitful function result can be stored for later use or placed directly where a value is needed.
The first assignment gives the returned value a name, which is useful when the value will be needed later. The second expression uses the returned value immediately as one part of a larger calculation. These are two common destinations for a fruitful function's return value: an assignment target or an expression.
Interactive Mode and Scripts
The same fruitful function call behaves differently in two working environments. In interactive mode, where commands are entered at the command-line interpreter and results appear immediately, Python automatically displays the value of an expression you type. In a saved script, Python still computes the value, but the result is not displayed unless your code explicitly prints it or assigns it to a variable.
2.236...| Where the call runs | What happens to the returned value | What you see |
|---|---|---|
| Interactive mode | Python evaluates the call and automatically displays the returned value. | The value appears immediately. |
| A script | Python evaluates the call, but display requires an explicit print or assignment. | Nothing is displayed by the call alone. |
Automatic display in interactive mode is a convenience feature, not a replacement for using the result in a script.
Choosing What to Do with the Result
Not every fruitful function call needs a separate variable. If the returned value is needed only once, use it directly in an expression or pass it to another function. If the value will be used more than once or referred to later, assign it to a variable with a meaningful name. If you are working interactively and only want to inspect the result, entering the call without an assignment lets Python display it automatically.
Common mistake Calling a fruitful function in a script and expecting its result to appear automatically.
What it looks likemath.sqrt(5)Why it failsA script computes the value but does not display it from the call alone.
Do this insteadExplicitly print the value when it must be displayed, or assign it when it must be used later.
Common mistake Calling a fruitful function without using the value it returns.
What it looks likeA function computes a result, but the script neither assigns nor uses that result.
Why it failsThe result is lost in the script when no later operation receives it.
Do this insteadCapture the result in a variable, use it in an expression, or pass it to another function.
Common mistake Treating a void function as though it produces a usable result.
What it looks likeAssigning the result of a function that only prints text.
Why it failsA void function performs its action but returns no value, so the assignment receives None.
Do this insteadUse the void function for its side effect instead of expecting a calculation result.
Common mistake Assuming that every fruitful result must first be assigned to a variable.
What it looks likeCreating a variable for a value that is needed only once in a larger expression.
Why it failsA returned value can be used directly wherever an expression value is accepted.
Do this insteadAssign only when a later reference or repeated use makes the name helpful.
If you assign the result of a void function, the variable receives None. None is Python's way of indicating that no value was returned. This is different from a fruitful function, whose call supplies data that can participate in later code.
Practice the Flow
What do you think happens?
Suppose math.sqrt(5) is entered directly at the interactive prompt. What should you expect?
Reveal answer
The returned value is displayed automatically.
Interactive mode automatically displays the value of an expression. In a script, the same call computes silently unless the code explicitly prints or assigns the result.
For each situation, decide what should happen to the return value: assign it to a variable, use it directly in an expression, pass it to another function, or display it interactively. Then explain why leaving a fruitful result unused in a script can be a problem.
Hints
- Ask whether the value is needed later or only once.
- Separate automatic interactive display from explicit script behavior.
- Ask whether the function produces data or only performs an action.
Key Takeaways
- 1A fruitful function returns a value to the code that called it.
- 2The returned value can be assigned to a variable or used directly in an expression.
- 3A void function performs an action without returning a usable value; assigning its result gives None.
- 4Interactive mode displays expression results automatically, while scripts require explicit printing or assignment.
- 5Choose whether to assign, use, pass, or display a return value according to what your code needs next.
At a glance
- Fruitful functions send values back to their callers.
- Return values are usable data: store them, include them in expressions, or pass them onward.
- Void functions perform actions and return no value; assigning their result produces None.
- Interactive mode displays a returned value automatically, but a script does not display it unless instructed.
- A returned value should have a clear destination whenever it matters to the program.