Unit 3: Programming · Procedures

Article

Tracing procedure calls

7 min read

Tracing means following a program by hand, one step at a time, and writing down what changes. It is the skill the AP exam uses most. With procedures, you also have to keep track of where you are: in the main program, or inside a procedure. This article gives you a method that always works.

Four steps for every call

  1. Skip the definitions. On your first pass, start at the first line that is not inside a procedure. A definition runs nothing.
  2. At a call, work out the arguments. Do the inside calls first. Copy each value into its parameter.
  3. Run the procedure's body. Stop at the first RETURN, or at the end of the body.
  4. Come back. Put the returned value where the call was, and carry on with the next step.

Here is a program with two procedures and a call inside a call:

AP pseudocode
PROCEDURE double(n)
{
    RETURN(n * 2)
}
PROCEDURE addOne(n)
{
    RETURN(n + 1)
}
x ← addOne(double(3))
DISPLAY(x)

Tracing addOne(double(3))

  1. 1

    Main program → double

    double(3)

  2. Inside double, n is 3, so RETURN(n * 2) gives 6.
  3. 2

    double → Main program

    returns 6

  4. 3

    Main program → addOne

    addOne(6)

  5. Inside addOne, n is 6, so RETURN(n + 1) gives 7.
  6. 4

    addOne → Main program

    returns 7

  7. Back in the main program, x gets 7. Then DISPLAY(x) shows 7.
The inside call double(3) runs first. Its answer becomes the argument of the outside call.

Both procedures have a parameter called n. That is fine. Each call has its own n, and the n of double is gone as soon as double finishes.

Check your understanding

What is displayed as a result of running this program?

AP pseudocode
PROCEDURE triple(n)
{
    RETURN(n * 3)
}
PROCEDURE minusOne(n)
{
    RETURN(n - 1)
}
DISPLAY(minusOne(triple(2)))

A trace table for a longer procedure

When a procedure has several variables, a table keeps them straight. Make one row for each call.

AP pseudocode
PROCEDURE mystery(a, b)
{
    c ← a * b
    IF(c > 10)
    {
        RETURN(c - 10)
    }
    DISPLAY("small")
    RETURN(c)
}
r ← mystery(2, 3)
s ← mystery(4, 5)
DISPLAY(r + s)
Runs in your browser
CallabcIs c > 10?What happensReturns
mystery(2, 3)236nosmall is displayed6
mystery(4, 5)4520yesthe procedure ends at the first RETURN10

Now r is 6 and s is 10, so the last line displays 16. The whole output is small and then 16. Notice that in the second call, the line DISPLAY("small") was never reached.

Check your understanding

What is displayed as a result of running this program?

AP pseudocode
PROCEDURE check(n)
{
    IF(n > 5)
    {
        RETURN("big")
    }
    DISPLAY("small")
    RETURN("done")
}
DISPLAY(check(8))
DISPLAY(check(2))

Worked example: a procedure that calls a procedure

AP pseudocode
PROCEDURE square(n)
{
    RETURN(n * n)
}
PROCEDURE sumOfSquares(a, b)
{
    RETURN(square(a) + square(b))
}
DISPLAY(sumOfSquares(3, 4))
  1. Skip both definitions. The first line outside them is DISPLAY(sumOfSquares(3, 4)).
  2. Call sumOfSquares(3, 4): a is 3 and b is 4.
  3. Inside it, work out square(a). This calls square(3), where n is 3, and returns 9.
  4. Then work out square(b). This calls square(4) and returns 16.
  5. The procedure returns 9 + 16, which is 25.
  6. DISPLAY shows 25.

Common mistakes

  • Running a procedure when you reach its definition. Defining does nothing. Wait for a call.
  • Using the wrong argument order. Copy the first argument into the first parameter.
  • Forgetting that the inside call of f(g(3)) runs first. g(3) is worked out before f starts.
  • Mixing up the n of two different procedures. Each call has its own parameters and variables. Use a separate row for each call.
  • Reading past a RETURN. The procedure stops at the first RETURN it reaches.
  • Losing a returned value. If the value is not stored or used, nothing changes in the program.

On the exam

"What is displayed as a result of running the program?" is the most common question about procedures. A few harder ones ask for the value of a variable after a call, or which argument makes a procedure return a certain value. Use the four steps. Write each call as a row in a table, and cross out any line that comes after a RETURN.

Key words

Trace: follow a program by hand, step by step, and record the value of each variable.

Trace table: a table with one row for each step or call and one column for each variable.

Flow of control: the order in which a program's instructions run, including jumps into a procedure and back.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.