Article
Tracing procedure calls
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
- Skip the definitions. On your first pass, start at the first line that is not inside a procedure. A definition runs nothing.
- At a call, work out the arguments. Do the inside calls first. Copy each value into its parameter.
- Run the procedure's body. Stop at the first
RETURN, or at the end of the body. - 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:
PROCEDURE double(n)
{
RETURN(n * 2)
}
PROCEDURE addOne(n)
{
RETURN(n + 1)
}
x ← addOne(double(3))
DISPLAY(x)Tracing addOne(double(3))
- 1
Main program → double
double(3)
- Inside double, n is 3, so RETURN(n * 2) gives 6.
- 2
double → Main program
returns 6
- 3
Main program → addOne
addOne(6)
- Inside addOne, n is 6, so RETURN(n + 1) gives 7.
- 4
addOne → Main program
returns 7
- Back in the main program, x gets 7. Then DISPLAY(x) shows 7.
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?
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.
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)| Call | a | b | c | Is c > 10? | What happens | Returns |
|---|---|---|---|---|---|---|
mystery(2, 3) | 2 | 3 | 6 | no | small is displayed | 6 |
mystery(4, 5) | 4 | 5 | 20 | yes | the procedure ends at the first RETURN | 10 |
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?
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
PROCEDURE square(n)
{
RETURN(n * n)
}
PROCEDURE sumOfSquares(a, b)
{
RETURN(square(a) + square(b))
}
DISPLAY(sumOfSquares(3, 4))- Skip both definitions. The first line outside them is
DISPLAY(sumOfSquares(3, 4)). - Call
sumOfSquares(3, 4):ais 3 andbis 4. - Inside it, work out
square(a). This callssquare(3), wherenis 3, and returns 9. - Then work out
square(b). This callssquare(4)and returns 16. - The procedure returns 9 + 16, which is 25.
DISPLAYshows25.
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 beforefstarts. - Mixing up the
nof 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.