Concepts / Using Built-in Functions Effectively

Using Built-in Functions Effectively

A function is a named sequence of statements that performs a computation. You call it by name to execute its statements.

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

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.

called withproducesfunction nameselects the functionargumentinput datareturn valueresult
Where are the function name, argument, and returned value located in a function call?

Following the Data

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. Identify the function: The name calculate identifies the named sequence of statements that will execute.

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

3. Follow the computation: The function uses the argument while performing its computation.

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

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

suppliesentersproducesreturns tocallerrequests a computation8argumentcalculatenamed sequence ofstatements16return value
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.

startscontinuesproducesfunction callname and argumentstatement 1part of the computationreturn valueresult sent backstatement 2part of the computation
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.

inputoutput5argumenttransformfunction computation10return value
How does the supplied argument determine the value returned by the function?

Mistakes in Function Tracing

  • Treating the function name as the result

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

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

  • Assuming the argument is automatically the return value

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

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

  • Ignoring what the caller can do with the result

    The return value is sent back to the calling code.

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

A Reliable Tracing Routine

  1. Read the function name and identify which named sequence of statements is being called.
  2. Locate the argument or arguments and record the input data supplied to the function.
  3. Follow the function's computation using those arguments.
  4. Identify the return value produced by the function.
  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. A function is a named sequence of statements that performs a computation.
  2. Calling a function by name causes its statements to execute.
  3. An argument is input data passed into the function.
  4. A return value is the result produced by the function and sent back to the caller.
  5. To predict a call's result, trace the function name, supplied argument, computation, and returned value in that order.

Key Takeaways

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