Working with the Math Module
In this assignment, math.cos(0) is the fruitful function call.
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).
- 1Use it once
Use
math.sqrt(5)directly when the returned value is immediately needed as part of a larger arithmetic expression. - 2Use it later
Assign the returned value to a meaningful variable when later code needs to refer to it or use it more than once.
- 3Check 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 |
A fruitful function still returns a value in both contexts; the difference is whether Python displays an unassigned expression automatically.
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
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 |
The key difference is what comes back to the caller.
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
Common mistake Assuming that seeing a result in interactive mode means a script will display it too.
What it looks likeA script contains math.sqrt(5) as a line by itself.Why it failsInteractive mode automatically displays expression results, but a script executes the call silently unless the result is explicitly printed or assigned.
Do this insteadIn a script, explicitly print the result when it must be shown, or assign it when later code must use it.
Common mistake Calling a fruitful function and then ignoring its returned value in a script.
What it looks likeA script calls a fruitful function without assigning, printing, or using its result.
Why it failsThe computation still happens, but the result is lost to the rest of the script.
Do this insteadCapture the value in a variable, use the call in an expression, pass it to another function, or explicitly print it.
Common mistake Treating every function call as though it produces a usable result.
What it looks likeA void function's call is assigned to a variable expecting computed data.
Why it failsVoid functions perform actions without returning a value, so the assigned result is None.
Do this insteadUse a void function for its side effect and use a fruitful function when the program needs returned data.
Common mistake Creating a variable for every returned value even when it is needed only once.
What it looks likeA returned value is immediately needed as part of a larger expression.
Why it failsThe extra variable is unnecessary when the function call can be used directly in the expression.
Do this insteadUse 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.
- 1Identify the function kind
Because the function is fruitful, its call produces a value that can be used by later code.
- 2Choose a destination
Assign the returned value to a variable so later code can refer to it.
- 3Check 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
- 1A fruitful function computes a value and sends it back to its caller.
- 2The returned value can be assigned to a variable, used directly in an expression, passed to another function, used in a condition, or displayed.
- 3Interactive 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.
- 4A void function performs an action without returning a value; assigning its result gives None.
- 5Choose 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.