Tracing a program that calls a procedure

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Tracing a program that calls a procedure

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A procedure is called with CALL ShowDouble(4). What is the first thing to record in the trace table?
A The final output of the whole program
B The value 4 stored in the parameter
2. PROCEDURE Greet(Name) is called by CALL Greet("Sam") and then CALL Greet("Mia"). What does the Name column show?
A "Sam" for the first call, "Mia" for the second call
B "Sam" for both calls
3. When the last statement of a procedure has finished, where does the trace go next?
A Back to the first line of the procedure
B To the line after the CALL in the main program
4. Main program: OUTPUT "Start", CALL Show(5), OUTPUT "End". Show(N) outputs N. In which order is the output shown?
A Start, 5, End
B Start, End, 5
5. A procedure has the local variable Total ← 0 as its first line. It is called twice. What is Total at the start of the second call?
A 0, because the local variable is reset each call
B The value left over from the first call

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1CALL ShowDouble(4) ACALL Square(2)\nCALL Square(5)
2CALL Greet("Sam")\nOUTPUT "Bye" BThe parameter must be given the value passed in the call (3), not 0 or a blank
3Two calls with different values need two sets of rows in the trace table: Num is 2 for the first call, then 5 for the second CIn the trace table, the parameter Num is given the value 4 before any statement in the procedure runs
4A local variable is reset in each call, so Total starts again from 0 every time the procedure runs DA local variable does not keep its value between calls; it starts fresh each time the procedure is called
5CALL Show(3) traced with Num = 0 in the first row EPROCEDURE Add(N : INTEGER)\n DECLARE Total : INTEGER\n Total ← 0 + N
6Trace of 2nd call: Count = 5 carried over from 1st call FThe greeting is output first, then control returns to the main program and Bye is output second

Definitions

trace table

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
____ Show(5) OUTPUT "Back in main"
2.
PROCEDURE Double(Num : INTEGER) OUTPUT ___ * 2 ENDPROCEDURE
3.
PROCEDURE Show(Num : INTEGER) OUTPUT Num ____________
4.
PROCEDURE AddTen(Num : INTEGER) Sum ← Num + 10 OUTPUT ___
5.
PROCEDURE Show(Num : _______) OUTPUT Num ENDPROCEDURE

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A PROCEDURE Double(N : INTEGER)
B DECLARE R : INTEGER
C R ← M * 2
D OUTPUT R
E ENDPROCEDURE
F CALL Double(4)
Question 2
A PROCEDURE Add(A, B : INTEGER)
B DECLARE Sum : INTEGER
C A + B ← Sum
D OUTPUT Sum
E ENDPROCEDURE
F CALL Add(2, 3)
Question 3
A PROCEDURE Greet(Name : STRING)
B OUTPUT "Hello ", "Name"
C ENDPROCEDURE
D CALL Greet("Ava")
Question 4
A PROCEDURE Show(N : INTEGER)
B OUTPUT N * N
C ENDPROCEDURE
D Show(3)
E CALL Show(5)
Question 5
A PROCEDURE Count(N : INTEGER)
B DECLARE T INTEGER
C T ← N + 1
D OUTPUT T
E ENDPROCEDURE
F CALL Count(7)

Answer Key

Tracing a program that calls a procedure
Section A: Quick Questions
Q1
B — The value 4 stored in the parameter
Q2
A — "Sam" for the first call, "Mia" for the second call
Q3
B — To the line after the CALL in the main program
Q4
A — Start, 5, End
Q5
A — 0, because the local variable is reset each call
Section B: Match the Pairs
Q1
CALL ShowDouble(4) → In the trace table, the parameter Num is given the value 4 before any statement in the procedure runs
Q2
CALL Greet("Sam")\nOUTPUT "Bye" → The greeting is output first, then control returns to the main program and Bye is output second
Q3
Two calls with different values need two sets of rows in the trace table: Num is 2 for the first call, then 5 for the second → CALL Square(2)\nCALL Square(5)
Q4
A local variable is reset in each call, so Total starts again from 0 every time the procedure runs → PROCEDURE Add(N : INTEGER)\n DECLARE Total : INTEGER\n Total ← 0 + N
Q5
CALL Show(3) traced with Num = 0 in the first row → The parameter must be given the value passed in the call (3), not 0 or a blank
Q6
Trace of 2nd call: Count = 5 carried over from 1st call → A local variable does not keep its value between calls; it starts fresh each time the procedure is called
Section C: Definitions
Q1
trace table — A table used to trace a call: record the value given to each parameter, follow the procedure's statements, then return to the next line of the main program.
Section D: Fill in the Blanks
Q1
CALL Show(5) OUTPUT "Back in main"
Q2
PROCEDURE Double(Num : INTEGER) OUTPUT Num * 2 ENDPROCEDURE
Q3
PROCEDURE Show(Num : INTEGER) OUTPUT Num ENDPROCEDURE
Q4
PROCEDURE AddTen(Num : INTEGER) Sum ← Num + 10 OUTPUT Sum
Q5
PROCEDURE Show(Num : INTEGER) OUTPUT Num ENDPROCEDURE
Section E: Spot the Error
Q1
C — The trace table has a column for the parameter N, which gets the value 4 from the call. M is never declared or given a value, so the line should be R ← N * 2.
Q2
C — Assignment puts the value on the right into the variable on the left. When tracing, Sum must be given A + B (2 + 3 = 5), so the line should be Sum ← A + B.
Q3
B — "Name" in quotes is the literal text Name. It would print Hello Name instead of the value held in the parameter. It should be OUTPUT "Hello ", Name so that Ava is output.
Q4
D — A procedure is run with the CALL keyword. Without it the first call never happens, so the trace has no row for N = 3. The line should be CALL Show(3).
Q5
B — A DECLARE statement needs a colon between the variable name and its data type. The local variable T should be declared as DECLARE T : INTEGER.