Debugging Function Calls
A function is a named sequence of statements that performs a computation. You call it by name to execute its statements.
The Call as a Handoff
When a program reaches a function call, control moves from the calling code to a named sequence of statements. The function uses the supplied input to perform its computation, then sends a result back to the calling code. Debugging a function call means tracing that handoff carefully: identify what was called, inspect the input, follow the statements, and check the value that comes back.
Three Parts to Inspect
Every function call has three key parts. First is the function name, which identifies the function whose statements will execute. Second is the argument, the value or variable passed into the function as input. Third is the return value, the result the function produces and sends back to the calling code. Once you separate these three parts, a confusing call becomes a traceable sequence: identify, pass, compute, and receive.
During debugging, write down the three parts separately. Ask: Which function name was called? What argument entered it? What return value came back? This prevents the common mistake of treating the input and the result as if they were the same value.
A Call in Motion
Tracing a Computation
A generated example uses a function named double. The function receives the argument 4, performs a computation that produces twice the input, and sends the result back to the caller. Trace the call.
- 1Identify the function
The function name is double. That name identifies the named sequence of statements that will execute.
- 2Identify the input
The argument is 4. It is the value passed into the function.
- 3Follow the computation
The function uses the argument to perform its work. In this generated example, the computation produces twice the input, so the result is 8.
- 4Follow the result
The return value is 8. The caller can display it, store it, or use it in a further computation.
The call sends 4 into double and receives 8 as the return value.
Predict Before Checking
What do you think happens?
A function named double receives the argument 7 and performs the same generated computation as the worked example. What return value should you expect?
Reveal answer
14
The argument is 7, and the generated function computation produces twice its input. The return value is therefore 14. The important debugging step is to distinguish the argument, 7, from the result, 14.
A reliable prediction follows the data, not just the function name. Start with the argument, apply the computation performed by the function's statements, and then identify the return value. The returned result may be displayed, stored, or used in another computation, so the value that comes back can become input to later code.
Debugging Checkpoints
Common mistake Treating the function name as the result
What it looks likeThe name identifies which named sequence of statements executes; it is not the return value.
Why it failsA function call uses the name to select the function, while the return value is the result produced by the function's work.
Do this insteadRecord the function name first, then trace the argument and the value returned.
Common mistake Confusing the argument with the return value
What it looks likeIn the generated double example, 4 is the argument and 8 is the return value.
Why it failsThe argument enters the function as input. The return value comes back after the function performs its computation.
Do this insteadLabel the input and output separately when tracing a call.
Common mistake Stopping the trace inside the function
What it looks likeFinding the result produced by the statements but not checking where that result goes.
Why it failsThe return value is sent back to the calling code, where it can be displayed, stored, or used in further computations.
Do this insteadContinue the trace beyond the function and identify how the caller uses the returned result.
Practice the Trace
A generated function named add_three receives the argument 5 and performs a computation that adds three to its input. Identify the function name, the argument, and the return value. Then state what the caller receives.
Hints
- The function name identifies the named sequence of statements.
- The argument is the value passed into the function.
- Apply the stated computation to the argument before identifying the return value.
Practice Solution
Use the generated add_three example.
- 1Function name
The function name is
add_three. - 2Argument
The argument is 5.
- 3Computation
The function adds three to its input, producing 8.
- 4Return value
The return value is 8, which is sent back to the caller.
The caller receives 8.
Trace Summary
- 1A function is a named sequence of statements that performs a computation.
- 2A function call has three key parts: the function name, the argument, and the return value.
- 3The argument is input data passed into the function.
- 4The return value is the result sent back to the calling code.
- 5To debug or predict a call, trace the name, input, computation, result, and how the caller uses that result.
At a glance
- A function is a named sequence of statements that performs a computation.
- Every function call can be examined through its function name, argument, and return value.
- Arguments move into a function as input, while return values move back to the caller as results.
- A dependable debugging trace follows the data from the caller, through the function's statements, and back to the caller.