Working with the Math Module
Fruitful functions return a value that flows back to the caller and can be captured in a variable or used in an expression.
A Result Must Go Somewhere
A function call can do more than make an action happen. A fruitful function computes something and sends a value back to the code that called it. That value may be a number, a string, a list, or another Python object. The returned value is useful only when your code captures it, places it in an expression, passes it to another function, or displays it.
Tracing a Math Result
In this assignment, math.cos(0) is the fruitful function call. The function computes the cosine of 0 radians and returns 1.0. Python then stores that returned value in x. The assignment is not storing the function itself; it is storing the result produced by the call.
A fruitful function call is an expression that produces a value. Once the value comes back, the surrounding code determines what happens next: an assignment can store it, or a larger expression can use it immediately.
Using Results in Larger Expressions
You do not always need a separate variable for a returned value. If a value is needed only once, a fruitful function call can be used directly inside a larger expression. The source example uses math.sqrt(5) as part of an arithmetic expression for the golden ratio. The square-root function returns approximately 2.236, and that returned number becomes one part of the larger calculation.
The call above produces approximately 2.236. In an expression, that value can continue through the calculation instead of being named first. If the result will be needed several times or referred to later, assigning it to a meaningful variable is usually the clearer choice. If it is needed once, direct use can avoid an unnecessary variable.
Choosing whether to assign
Decide how to use the value produced by math.sqrt(5).
Use it once: Use math.sqrt(5) directly when the returned value is immediately needed as part of a larger arithmetic expression.
Use it later: Assign the returned value to a meaningful variable when later code needs to refer to it or use it more than once.
Check the destination: Whether the value is assigned, used in an expression, passed onward, or displayed, the important point is that the returned result is used rather than discarded.
The right choice depends on how long and how often the program needs the returned value.
Interactive Mode and Scripts
The same fruitful function call can appear to behave differently depending on where it runs. In interactive mode, Python displays the return value of an expression that you enter. This makes interactive mode convenient for trying a call and immediately seeing its result.
When math.sqrt(5) is entered directly into the interactive interpreter, Python evaluates the call and displays the returned value on the next line. You did not assign the value or print it explicitly; interactive mode displayed it as a convenience.
| Context | What happens to the returned value | What you must do to display it |
|---|---|---|
| Interactive mode | Python automatically displays the value of the expression | Nothing extra is required just to see the value |
| Script | The function call executes, but the result is not displayed automatically | Explicitly print or assign the result if the program needs to display or use it |
What do you think happens?
What happens when the same math.sqrt(5) call runs in a saved script without an explicit print or assignment?
Reveal answer
Answer: The function computes and returns the value, but the script does not display it automatically.
Automatic display is a convenience of interactive mode. In a script, the result must be explicitly printed or assigned if the program needs to show or retain it.
Fruitful and Void Functions
| Function kind | What it does | How its call is used |
|---|---|---|
| Fruitful function | Performs a computation and sends a value back | Use the result in an assignment, expression, condition, function argument, or display |
| Void function | Performs an action without returning a value | Use it for its side effect rather than for a result |
A void function can perform an action such as printing text or writing to a file, then stop without sending a useful result back. If you assign the result of a void function to a variable, that variable receives None, Python's way of indicating that no value was returned.
Completed
NoneMistakes That Lose Results
Assuming that seeing a result in interactive mode means a script will display it too.
Interactive mode automatically displays expression results, but a script executes the call silently unless the result is explicitly printed or assigned.
Fix:
In a script, explicitly print the result when it must be shown, or assign it when later code must use it.Calling a fruitful function and then ignoring its returned value in a script.
The computation still happens, but the result is lost to the rest of the script.
Fix:
Capture the value in a variable, use the call in an expression, pass it to another function, or explicitly print it.Treating every function call as though it produces a usable result.
Void functions perform actions without returning a value, so the assigned result is None.
Fix:
Use a void function for its side effect and use a fruitful function when the program needs returned data.Creating a variable for every returned value even when it is needed only once.
The extra variable is unnecessary when the function call can be used directly in the expression.
Fix:
Use the call directly when the value is needed once; assign it when later or repeated use makes a name helpful.
Practice the Return Path
For each situation, decide whether the returned value should be assigned, used directly in an expression, displayed, or ignored because the function is being used for its action. First, try to answer without looking back at the explanations.
Hints
- Ask whether the function sends a value back or only performs an action.
- Ask whether the code is running interactively or inside a script.
- Ask whether later code needs the value or whether it is needed only once.
Checking a call's destination
A fruitful function is called in a script. The program needs the returned value later, but it should not display anything immediately.
Identify the function kind: Because the function is fruitful, its call produces a value that can be used by later code.
Choose a destination: Assign the returned value to a variable so later code can refer to it.
Check display behavior: Because this is a script, assigning the value does not automatically display it. Display would require an explicit print operation.
The returned value is captured for later use without being displayed automatically.
Explain in your own words why math.sqrt(5) produces visible output when entered directly in interactive mode but produces no automatic visible output when the same call appears alone in a script.
Hints
- Separate the act of returning a value from the act of displaying a value.
- Remember which context automatically displays expression results.
Working Rule
- A fruitful function computes a value and sends it back to its caller.
- The returned value can be assigned to a variable, used directly in an expression, passed to another function, used in a condition, or displayed.
- Interactive mode displays the value of an entered expression automatically, while a script requires explicit printing or assignment for the result to be shown or retained.
- A void function performs an action without returning a value; assigning its result gives None.
- Choose whether to assign a result based on whether later code needs to refer to it, use it repeatedly, or use it only once immediately.
Key Takeaways
- Fruitful functions return real Python data to the point where they were called.
- A returned value does not disappear when it is placed in an assignment or larger expression.
- Interactive mode displays unassigned expression results automatically; scripts do not.
- Void functions are used for their actions and produce None when their result is assigned.
- Use a returned value directly when it is needed once, or assign it when later code needs a meaningful reference.