Concepts / Debugging Function Calls

Debugging Function Calls

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

  • Programming

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.

Callercalling codeArgumentinput dataFunctionnamed statementsReturn valuecomputed resultUse resultdisplay, store, or computepassesentersproducessends back
How does data move from the caller into a function, through its statements, and back as a return value?

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.

function nameidentifies the functionargumentinput datareturn valueresult from the functionreceiveshelps produce
Which part of a function call identifies the function, which part supplies input, and where does the returned value go?
Best practice

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

Worked example

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.

  1. 1
    Identify the function

    The function name is double. That name identifies the named sequence of statements that will execute.

  2. 2
    Identify the input

    The argument is 4. It is the value passed into the function.

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

  4. 4
    Follow the result

    The return value is 8. The caller can display it, store it, or use it in a further computation.

Result

The call sends 4 into double and receives 8 as the return value.

Call doublefunction name4argumentFunction statementsperform computation8return valueCallerreceives resulttakesentersproducesreturns to
What happens after a function is called, and in what order do its statements execute before control returns to the caller?

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?

  • 7

  • 14

  • The function name

  • The argument is returned unchanged

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.

7argument14return valuedoublefunction nameCallerreceives resultusesbecomesreturns to
What value comes out of the function after the argument is passed in and the statements execute?

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 like

    The name identifies which named sequence of statements executes; it is not the return value.

    Why it fails

    A function call uses the name to select the function, while the return value is the result produced by the function's work.

    Do this instead

    Record the function name first, then trace the argument and the value returned.

  • Common mistake

    Confusing the argument with the return value

    What it looks like

    In the generated double example, 4 is the argument and 8 is the return value.

    Why it fails

    The argument enters the function as input. The return value comes back after the function performs its computation.

    Do this instead

    Label the input and output separately when tracing a call.

  • Common mistake

    Stopping the trace inside the function

    What it looks like

    Finding the result produced by the statements but not checking where that result goes.

    Why it fails

    The return value is sent back to the calling code, where it can be displayed, stored, or used in further computations.

    Do this instead

    Continue the trace beyond the function and identify how the caller uses the returned result.

Practice the Trace

EASY

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.
Worked example

Practice Solution

Use the generated add_three example.

  1. 1
    Function name

    The function name is add_three.

  2. 2
    Argument

    The argument is 5.

  3. 3
    Computation

    The function adds three to its input, producing 8.

  4. 4
    Return value

    The return value is 8, which is sent back to the caller.

Result

The caller receives 8.

Trace Summary

  1. 1
    A function is a named sequence of statements that performs a computation.
  2. 2
    A function call has three key parts: the function name, the argument, and the return value.
  3. 3
    The argument is input data passed into the function.
  4. 4
    The return value is the result sent back to the calling code.
  5. 5
    To 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.

Related concepts

More concepts starting with D