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.
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.
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.
- 11. Identify the function
The name calculate identifies the named sequence of statements that will execute.
- 22. Identify the input
The value 8 is the argument. It is the input data supplied to the function.
- 33. Follow the computation
The function uses the argument while performing its computation.
- 44. 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.
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.
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?
Reveal 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.
Mistakes in Function Tracing
Common mistake Treating the function name as the result
What it looks likeA call uses the name calculate, but the function name is only the identifier for the named sequence of statements.
Why it failsThe return value is the result produced by the function, not the name used to call it.
Do this insteadTrace 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 likeA function receives 5 and returns 10.
Why it failsThe argument is input data. The function uses it while performing its computation, and the return value is the resulting output.
Do this insteadLook for the result produced after the function performs its work.
Common mistake Ignoring what the caller can do with the result
What it looks likeA return value is treated as if it disappears after the function finishes.
Why it failsThe return value is sent back to the calling code.
Do this insteadRemember that returned data can be displayed, stored, or used in further computations.
A Reliable Tracing Routine
- 1Read the function name and identify which named sequence of statements is being called.
- 2Locate the argument or arguments and record the input data supplied to the function.
- 3Follow the function's computation using those arguments.
- 4Identify the return value produced by the function.
- 5Use the return value as the result available to the calling code.
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
- 1A function is a named sequence of statements that performs a computation.
- 2Calling a function by name causes its statements to execute.
- 3An argument is input data passed into the function.
- 4A return value is the result produced by the function and sent back to the caller.
- 5To 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.