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.
Identify the function: The function name is double. That name identifies the named sequence of statements that will execute.
Identify the input: The argument is 4. It is the value passed into the function.
Follow 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.
Follow 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
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
Treating the function name as the result
A function call uses the name to select the function, while the return value is the result produced by the function's work.
Fix:
Record the function name first, then trace the argument and the value returned.Confusing the argument with the return value
The argument enters the function as input. The return value comes back after the function performs its computation.
Fix:
Label the input and output separately when tracing a call.Stopping the trace inside the function
The return value is sent back to the calling code, where it can be displayed, stored, or used in further computations.
Fix:
Continue 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.
Function name: The function name is add_three.
Argument: The argument is 5.
Computation: The function adds three to its input, producing 8.
Return value: The return value is 8, which is sent back to the caller.
The caller receives 8.
Trace Summary
- A function is a named sequence of statements that performs a computation.
- A function call has three key parts: the function name, the argument, and the return value.
- The argument is input data passed into the function.
- The return value is the result sent back to the calling code.
- To debug or predict a call, trace the name, input, computation, result, and how the caller uses that result.
Key Takeaways
- 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.