Concepts / Working with the Math Module

Working with the Math Module

In this assignment, math.cos(0) is the fruitful function call.

  • Programming

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.

Calling codefunction callFruitful functioncomputes a resultReturned valuereal Python dataAssignment orexpressionvalue continues herecallsreturnsflows to
How does a value produced inside a fruitful function travel back to the calling code?

Tracing a Math Result

python

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.

math.cos(0)function call1.0returned valuexstored valuereturnsassigned to
Where can the value returned by a fruitful function be placed so later code can use it?
Key point

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.

python

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.

assignmentstore the resultreturned valuefrom a fruitful callcalculationcombine the resultconditiontest the resultfunction argumentpass the resultcan entercan entercan entercan enter
In which parts of a program can a returned value participate, such as an assignment, calculation, condition, or function argument?
Worked example

Choosing whether to assign

Decide how to use the value produced by math.sqrt(5).

  1. 1
    Use it once

    Use math.sqrt(5) directly when the returned value is immediately needed as part of a larger arithmetic expression.

  2. 2
    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.

  3. 3
    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.

Result

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.

python

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.

ContextWhat happens to the returned valueWhat you must do to display it
Interactive modePython automatically displays the value of the expressionNothing extra is required just to see the value
ScriptThe function call executes, but the result is not displayed automaticallyExplicitly 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.

math.sqrt(5)interactive mode2.236...displayedmath.sqrt(5)script2.236...not displayed automaticallyreturns and displaysreturns silently
What happens to a fruitful function's returned value when the same call runs interactively compared with in a script?

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 kindWhat it doesHow its call is used
Fruitful functionPerforms a computation and sends a value backUse the result in an assignment, expression, condition, function argument, or display
Void functionPerforms an action without returning a valueUse 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.

python
Output
Completed
None
Fruitful functionvalue comes backPython objectusable resultVoid functionaction occursNoneno value returnedreturnsproduces
What is the difference between a function that sends a value back and one that performs an action without returning a result?

Mistakes That Lose Results

  • Common mistake

    Assuming that seeing a result in interactive mode means a script will display it too.

    What it looks like
    A script contains math.sqrt(5) as a line by itself.
    Why it fails

    Interactive mode automatically displays expression results, but a script executes the call silently unless the result is explicitly printed or assigned.

    Do this instead

    In 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 like

    A script calls a fruitful function without assigning, printing, or using its result.

    Why it fails

    The computation still happens, but the result is lost to the rest of the script.

    Do this instead

    Capture 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 like

    A void function's call is assigned to a variable expecting computed data.

    Why it fails

    Void functions perform actions without returning a value, so the assigned result is None.

    Do this instead

    Use 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 like

    A returned value is immediately needed as part of a larger expression.

    Why it fails

    The extra variable is unnecessary when the function call can be used directly in the expression.

    Do this instead

    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

MEDIUM

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.
Worked example

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.

  1. 1
    Identify the function kind

    Because the function is fruitful, its call produces a value that can be used by later code.

  2. 2
    Choose a destination

    Assign the returned value to a variable so later code can refer to it.

  3. 3
    Check display behavior

    Because this is a script, assigning the value does not automatically display it. Display would require an explicit print operation.

Result

The returned value is captured for later use without being displayed automatically.

EASY

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

  1. 1
    A fruitful function computes a value and sends it back to its caller.
  2. 2
    The returned value can be assigned to a variable, used directly in an expression, passed to another function, used in a condition, or displayed.
  3. 3
    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.
  4. 4
    A void function performs an action without returning a value; assigning its result gives None.
  5. 5
    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.

Related concepts

More concepts starting with W