← IGCSE Computer Science Paper 2

Tracing a program that calls a function

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.

Understanding Tracing a program that calls a function in IGCSE Computer Science Paper 2

What it does: The call is replaced by 500, so Total holds 500 after the assignment. The function returns 9 + 16 = 25, the call is replaced by 25, and 25 is output. The whole call SumSquare(10, 20) is replaced by the returned value 500. Then you finish the expression: 500 + 1 = 501. 7 MOD 2 = 1, so 1 = 0 is FALSE. The call IsEven(7) is replaced by FALSE, so the IF takes the ELSE branch and outputs Odd. RETURN sends the value back and ends the function straight away. Tracing continues in the code that made the call.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 419–428) and word search (Modules 419–438).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
FUNCTION SumSquare(A, B) RETURNS A * A + B * B. What does OUTPUT SumSquare(10, 20) display?
30500
Matches
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.
Applying
Meanings
Term
SumSquare(10, 20)
Meaning
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.
Practice
FUNCTION SumSquare(A : INTEGER, B : INTEGER) RETURNS INTEGER
Testing
Gaps
OUTPUT
Check Answer
Spots
1PROCEDURE Sq(N)
2 RETURN N * N

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Walrus' badge — Method Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. FUNCTION SumSquare(A, B) RETURNS A * A + B * B. What does OUTPUT SumSquare(10, 20) display?
A. 30
B. 500 ✓
Follow the function to RETURN: 10 * 10 + 20 * 20 = 100 + 400 = 500. The call is replaced by 500, so 500 is output.
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
The whole call SumSquare(10, 20) is replaced by the returned value 500. Then you finish the expression: 500 + 1 = 501.
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
You must work through the function's statements to the RETURN to find the value. Then that value takes the place of the call.
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
7 MOD 2 = 1, so 1 = 0 is FALSE. The call IsEven(7) is replaced by FALSE, so the IF takes the ELSE branch and outputs Odd.
5. Total ← SumSquare(1, 2). SumSquare returns A * A + B * B. What value is stored in Total?
A. 3
B. 5 ✓
The function returns 1 * 1 + 2 * 2 = 5. The call is replaced by 5, and that value is stored in Total.

Match the Pairs

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.
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.
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)
Tracing a function inside OUTPUT: replace the call with the returned value, then output that value. → OUTPUT SumSquare(3, 4)
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.
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.

Key Term

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.

Fill in the Blanks

1. Total ← SumSquare(10, 20) OUTPUT Total
Hint: Type the variable that now holds the value the function returned.
2. FUNCTION SumSquare(A, B) RETURNS INTEGER RETURN A * A + B * B ENDFUNCTION
Hint: Type the keyword that sends the result back to the place where the function was called.
3. IF IsEven(8) = TRUE THEN OUTPUT "Even" ENDIF
Hint: Type the BOOLEAN value the call is replaced by when it returns, so the condition is satisfied.
4. Total ← SumSquare(10, 20) + 1 // the call is replaced by 500 // so Total is 501
Hint: Work out 10 squared plus 20 squared and type the number the call is replaced by.
5. // SumSquare returns A * A + B * B OUTPUT SumSquare(10, 20) // the call is replaced by 500
Hint: Type the name of the function whose call is traced and replaced by its returned value.

Spot the Error

Question 1
A PROCEDURE Sq(N)
B RETURN N * N
C ENDFUNCTION
D OUTPUT Sq(4)
Line 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.
Question 2
A FUNCTION Sq(N) RETURNS STRING
B RETURN N * N
C ENDFUNCTION
D X ← Sq(3) + 1
Line 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.
Question 3
A FUNCTION Ok(N) RETURNS BOOLEAN
B RETURN N >= 50
C ENDFUNCTION
D IF Ok() THEN
E OUTPUT "Pass"
F ENDIF
Line 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.
Question 4
A FUNCTION Sq(N) RETURNS INTEGER
B OUTPUT N * N
C ENDFUNCTION
D OUTPUT Sq(5) + 1
Line 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.
Question 5
A FUNCTION Sq(N) RETURNS INTEGER
B RETURN N * N
C ENDPROCEDURE
D Result ← Sq(6)
E OUTPUT Result
Line C — A function must be closed with ENDFUNCTION, not ENDPROCEDURE. Sq(6) is replaced by 36, which is then stored in Result.

Student with a class code? Join your class →