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