Tracing a program that calls a function

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

Tracing a program that calls a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. FUNCTION SumSquare(A, B) RETURNS A * A + B * B. What does OUTPUT SumSquare(10, 20) display?
A 30
B 500
2. Tracing OUTPUT SumSquare(10, 20) + 1. The function returns 500. What expression is left to finish evaluating?
A 500 + 1, which gives 501
B SumSquare + 1, which gives 31
3. What is the correct way to trace a function call?
A Follow its statements to the RETURN, then replace the call with the returned value
B Skip the function body and use the argument values as the result
4. IsEven(N) RETURNS N MOD 2 = 0. What is output by: IF IsEven(7) THEN OUTPUT "Even" ELSE OUTPUT "Odd" ENDIF?
A Odd
B Even
5. Total ← SumSquare(1, 2). SumSquare returns A * A + B * B. What value is stored in Total?
A 3
B 5

Match the Pairs

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

Left columnRight column
1OUTPUT SumSquare(10, 20) + 1 AThe student added the arguments and ignored the function's statements. They must follow the statements to RETURN: 10*10 + 20*20 = 500.
2IF IsEven(7) THEN B7 MOD 2 is 1, so the function returns FALSE. The call is replaced by FALSE and the THEN block is skipped.
3A function result stored in a variable: the call is replaced by the returned value, which is then assigned to the variable. CThe call was replaced by 13 but the rest of the expression was not finished. 13 + 1 must be evaluated, so 14 is output.
4Tracing a function inside OUTPUT: replace the call with the returned value, then output that value. DOUTPUT SumSquare(3, 4)
5SumSquare(10, 20) is replaced by 30 ETotal ← SumSquare(10, 20)
6OUTPUT SumSquare(2, 3) + 1 shows 13 FThe call returns 500 (100 + 400). The call is replaced by 500 and the expression is finished, so 501 is output.

Definitions

SumSquare(10, 20)

Fill in the Blanks

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

1.
Total ← SumSquare(10, 20) OUTPUT _____
2.
FUNCTION SumSquare(A, B) RETURNS INTEGER ______ A * A + B * B ENDFUNCTION
3.
IF IsEven(8) = ____ THEN OUTPUT "Even" ENDIF
4.
Total ← SumSquare(10, 20) + 1 // the call is replaced by ___ // so Total is 501
5.
// SumSquare returns A * A + B * B OUTPUT _________(10, 20) // the call is replaced by 500

Spot the Error

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

Question 1
A PROCEDURE Sq(N)
B RETURN N * N
C ENDFUNCTION
D OUTPUT Sq(4)
Question 2
A FUNCTION Sq(N) RETURNS STRING
B RETURN N * N
C ENDFUNCTION
D X ← Sq(3) + 1
Question 3
A FUNCTION Ok(N) RETURNS BOOLEAN
B RETURN N >= 50
C ENDFUNCTION
D IF Ok() THEN
E OUTPUT "Pass"
F ENDIF
Question 4
A FUNCTION Sq(N) RETURNS INTEGER
B OUTPUT N * N
C ENDFUNCTION
D OUTPUT Sq(5) + 1
Question 5
A FUNCTION Sq(N) RETURNS INTEGER
B RETURN N * N
C ENDPROCEDURE
D Result ← Sq(6)
E OUTPUT Result

Answer Key

Tracing a program that calls a function
Section A: Quick Questions
Q1
B — 500
Q2
A — 500 + 1, which gives 501
Q3
A — Follow its statements to the RETURN, then replace the call with the returned value
Q4
A — Odd
Q5
B — 5
Section B: Match the Pairs
Q1
OUTPUT SumSquare(10, 20) + 1 → The call returns 500 (100 + 400). The call is replaced by 500 and the expression is finished, so 501 is output.
Q2
IF IsEven(7) THEN → 7 MOD 2 is 1, so the function returns FALSE. The call is replaced by FALSE and the THEN block is skipped.
Q3
A function result stored in a variable: the call is replaced by the returned value, which is then assigned to the variable. → Total ← SumSquare(10, 20)
Q4
Tracing a function inside OUTPUT: replace the call with the returned value, then output that value. → OUTPUT SumSquare(3, 4)
Q5
SumSquare(10, 20) is replaced by 30 → The student added the arguments and ignored the function's statements. They must follow the statements to RETURN: 10*10 + 20*20 = 500.
Q6
OUTPUT SumSquare(2, 3) + 1 shows 13 → The call was replaced by 13 but the rest of the expression was not finished. 13 + 1 must be evaluated, so 14 is output.
Section C: Definitions
Q1
SumSquare(10, 20) — Tracing a call: follow the function's statements to RETURN, then replace the call with the returned value (here 500) and finish evaluating the expression.
Section D: Fill in the Blanks
Q1
Total ← SumSquare(10, 20) OUTPUT Total
Q2
FUNCTION SumSquare(A, B) RETURNS INTEGER RETURN A * A + B * B ENDFUNCTION
Q3
IF IsEven(8) = TRUE THEN OUTPUT "Even" ENDIF
Q4
Total ← SumSquare(10, 20) + 1 // the call is replaced by 500 // so Total is 501
Q5
// SumSquare returns A * A + B * B OUTPUT SumSquare(10, 20) // the call is replaced by 500
Section E: Spot the Error
Q1
A — Sq returns a value that OUTPUT uses, so it must be a FUNCTION with RETURNS INTEGER. A procedure cannot return a value. The call Sq(4) is replaced by 16 and then output.
Q2
A — The function returns N * N, which is a whole number, so the return type must be INTEGER, not STRING. Sq(3) is replaced by 9, then 1 is added to give 10.
Q3
D — The call needs its argument, so it should be IF Ok(70) THEN. Ok(70) is replaced by TRUE, because 70 >= 50, and then the IF condition is evaluated.
Q4
B — A function must RETURN its result, not OUTPUT it. It should be RETURN N * N, so that Sq(5) is replaced by 25 and the expression 25 + 1 gives 26.
Q5
C — A function must be closed with ENDFUNCTION, not ENDPROCEDURE. Sq(6) is replaced by 36, which is then stored in Result.