Concepts / Using Built-in Functions Effectively

Using Built-in Functions Effectively

A function call is not just a name. It connects a function name, input data called an argument, and an output called the return value.

  • Programming

A Call Starts the Work

When your code needs a computation, it can call a function by name. The call tells the function to execute its named sequence of statements. To understand what happens, follow the data in two directions: an argument travels into the function, and the function's return value travels back to the calling code.

Key point

A function call is not just a name. It connects a function name, input data called an argument, and an output called the return value.

The Three-Part Call

Every function call has three key parts. The function name identifies which named sequence of statements should execute. The argument supplies the input data; it may be a value or a variable. The return value is the result produced by the function and sent back to the calling code. Once returned, that result can be displayed, stored, or used in another computation.

function nameselects the functionargumentinput datareturn valueresultcalled withproduces
Where are the function name, argument, and returned value located in a function call?

Following the Data

Worked example

Tracing One Function Call

Suppose a function named calculate is called with the argument 8. During its computation, it produces the return value 16. Trace the movement of the data.

  1. 1
    1. Identify the function

    The name calculate identifies the named sequence of statements that will execute.

  2. 2
    2. Identify the input

    The value 8 is the argument. It is the input data supplied to the function.

  3. 3
    3. Follow the computation

    The function uses the argument while performing its computation.

  4. 4
    4. Follow the result

    The function produces 16 as its return value and sends that result back to the calling code.

Result

The call begins with the function name calculate and the argument 8, then produces the return value 16.

callerrequests a computation8argumentcalculatenamed sequence ofstatements16return valuesuppliesentersproducesreturns to
How does data move from the caller into a function and how does the result move back?

Inside the Function

Calling a function causes its statements to execute. Those statements perform the computation using the supplied argument. The important boundary is between the caller and the function: the caller provides input, the function performs its work, and the function sends a result back through its return value.

function callname and argumentstatement 1part of the computationreturn valueresult sent backstatement 2part of the computationstartscontinuesproduces
What happens after a function is called, and in what order do its statements execute?

Predicting the Result

What do you think happens?

A function named transform receives the argument 5. Suppose its computation produces the return value 10. What result does the caller receive?

  • 5

  • 10

  • transform

  • No result

Reveal answer
Answer:

10

The argument is the input supplied to the function. The return value is the result produced by the function and sent back to the caller, so the caller receives 10.

5argumenttransformfunction computation10return valueinputoutput
How does the supplied argument determine the value returned by the function?

Mistakes in Function Tracing

  • Common mistake

    Treating the function name as the result

    What it looks like

    A call uses the name calculate, but the function name is only the identifier for the named sequence of statements.

    Why it fails

    The return value is the result produced by the function, not the name used to call it.

    Do this instead

    Trace the name to the function, then identify the value that the function returns.

  • Common mistake

    Assuming the argument is automatically the return value

    What it looks like

    A function receives 5 and returns 10.

    Why it fails

    The argument is input data. The function uses it while performing its computation, and the return value is the resulting output.

    Do this instead

    Look for the result produced after the function performs its work.

  • Common mistake

    Ignoring what the caller can do with the result

    What it looks like

    A return value is treated as if it disappears after the function finishes.

    Why it fails

    The return value is sent back to the calling code.

    Do this instead

    Remember that returned data can be displayed, stored, or used in further computations.

A Reliable Tracing Routine

  1. 1
    Read the function name and identify which named sequence of statements is being called.
  2. 2
    Locate the argument or arguments and record the input data supplied to the function.
  3. 3
    Follow the function's computation using those arguments.
  4. 4
    Identify the return value produced by the function.
  5. 5
    Use the return value as the result available to the calling code.
EASY

A function named process is called with the argument 12. Its computation produces the return value 7. Identify the function name, the argument, and the return value, then describe the direction of data flow.

Hints
  • The function name identifies the named sequence of statements.
  • The argument is the input value supplied to the function.
  • The return value is the result sent back to the caller.

What to Remember

  1. 1
    A function is a named sequence of statements that performs a computation.
  2. 2
    Calling a function by name causes its statements to execute.
  3. 3
    An argument is input data passed into the function.
  4. 4
    A return value is the result produced by the function and sent back to the caller.
  5. 5
    To predict a call's result, trace the function name, supplied argument, computation, and returned value in that order.

At a glance

  • Functions package a computation as a named sequence of statements.
  • Every function call can be understood through its function name, argument, and return value.
  • Arguments carry input data into a function, while return values carry results back to the caller.
  • A dependable prediction method is to trace the call, input, computation, and output in sequence.

Related concepts

More concepts starting with U