Concepts / Using Built-in Functions

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.

  • Programming

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.

Calling codefunction callmath.sqrt(5)computation2.236...returned valuelarger expressionvalue is usedcallsreturnsflows into
How does a value produced inside a fruitful function move back to the calling code?
Key point

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.

Fruitful functionreturns a valueVoid functionperforms an actionresultusable dataNoneno value returnedsends backassignment receives
What is the difference between a function that returns a value and one that only performs an action?
Function kindWhat it doesWhat comes backTypical reason to call it
FruitfulPerforms a computationA valueTo use the result in later code
VoidPerforms an actionNo value; assigning its result gives NoneTo produce a side effect

The distinction is whether a usable value returns to the caller.

Capturing a Function Result

Worked example

Using Two Mathematical Results

How can the results of fruitful built-in function calls be used in later code?

  1. 1
    Store 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.

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

  3. 3
    Keep the purpose in view

    In both cases, the function's result is captured or used. The returned value does not disappear.

Result

A fruitful function result can be stored for later use or placed directly where a value is needed.

python

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.

xvariable=assignmentmath.cos(0)fruitful call1.0returned valuelarger expressionuses returned valuereturnsassigned toused in
Where can the value returned by a fruitful function go, and how can it be used in an assignment or 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.

python
Output
2.236...
Where the call runsWhat happens to the returned valueWhat you see
Interactive modePython evaluates the call and automatically displays the returned value.The value appears immediately.
A scriptPython 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.

Interactive modemath.sqrt(5)Scriptmath.sqrt(5)2.236...displayed automaticallycomputed resultnot displayed automaticallydisplayscomputes
What happens to a returned value when a fruitful function is called at the interactive prompt compared with inside 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 like
    math.sqrt(5)
    Why it fails

    A script computes the value but does not display it from the call alone.

    Do this instead

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

    A function computes a result, but the script neither assigns nor uses that result.

    Why it fails

    The result is lost in the script when no later operation receives it.

    Do this instead

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

    Assigning the result of a function that only prints text.

    Why it fails

    A void function performs its action but returns no value, so the assignment receives None.

    Do this instead

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

    Creating a variable for a value that is needed only once in a larger expression.

    Why it fails

    A returned value can be used directly wherever an expression value is accepted.

    Do this instead

    Assign only when a later reference or repeated use makes the name helpful.

Edge case

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?

  • The returned value is displayed automatically.

  • The function does not run.

  • The value is displayed only if it was assigned first.

Reveal answer
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.

MEDIUM

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

  1. 1
    A fruitful function returns a value to the code that called it.
  2. 2
    The returned value can be assigned to a variable or used directly in an expression.
  3. 3
    A void function performs an action without returning a usable value; assigning its result gives None.
  4. 4
    Interactive mode displays expression results automatically, while scripts require explicit printing or assignment.
  5. 5
    Choose 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.

Related concepts

More concepts starting with U