← IGCSE Computer Science Paper 2

Finding and fixing errors in subroutine code

Finding and fixing errors by checking a subroutine's header, parameter types, RETURN, CALL, end keyword and variable scope against the syntax rules.

Understanding Finding and fixing errors in subroutine code in IGCSE Computer Science Paper 2

What it does: This function is correct because it has a RETURN and finishes with the matching end keyword ENDFUNCTION. Result is local, so it cannot be used outside the function. RETURN Result sends its value back to the caller. The number of values in a call must match the number of parameters. The correct call is CALL Greet("Sam", 15). A function call is used inside an expression, such as OUTPUT or an assignment. CALL is used for procedures. The header lists one parameter, so the call must give exactly one argument, such as CALL ShowTotal(5). Local variables only exist within their own subroutine. Moving OUTPUT Sum inside the procedure fixes the scope error.

Common mistakes: The function header is missing the RETURNS keyword. A function header must state the return type as RETURNS INTEGER. The parameter Name has no data type. Each parameter in the header needs a type, so it should be Name : STRING.

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
A function begins FUNCTION Double(N : INTEGER) RETURNS INTEGER. Its last line is RETURN N * 2 and nothing follows it. What is the error?
ENDFUNCTION is missingCALL is missing
Matches
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18
↓
The ENDFUNCTION keyword is missing, so the function is never closed. Add ENDFUNCTION on the last line.
Applying
Meanings
Term
Debugging
Meaning
Finding and fixing errors by checking a subroutine's header, parameter types, RETURN, CALL, end keyword and variable scope against the syntax rules.
Practice
FUNCTION AddTax(Price : REAL) RETURNS REAL
Testing
Gaps
Check Answer
Spots
1FUNCTION Six() INTEGER
2 RETURN 6

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

The 'Arctic Fox' badge — Tundra Tracker, 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. A function begins FUNCTION Double(N : INTEGER) RETURNS INTEGER. Its last line is RETURN N * 2 and nothing follows it. What is the error?
A. CALL is missing
B. ENDFUNCTION is missing ✓
Every FUNCTION must be closed with ENDFUNCTION. Check that the end keyword matches the start keyword.
2. FUNCTION Total(A : INTEGER, B : INTEGER) RETURNS INTEGER has the line Sum <- A + B, then ENDFUNCTION. What is wrong?
A. The parameters must be REAL
B. There is no RETURN statement ✓
A function must send a value back with RETURN. Here it should be RETURN Sum.
3. PROCEDURE Greet(Name : STRING, Age : INTEGER) is called with CALL Greet("Sam"). What is the problem?
A. Wrong number of values: 2 are needed but 1 is given ✓
B. CALL should be replaced by RETURN
The number of values in a call must match the number of parameters. The correct call is CALL Greet("Sam", 15).
4. Total is declared inside PROCEDURE Add. A line outside Add uses OUTPUT Total. Why does this cause an error?
A. Total is local, so it cannot be used outside Add ✓
B. OUTPUT cannot display an INTEGER
A local variable only exists inside its own subroutine. Return the value from a function, or declare the variable globally.
5. Greet has the header PROCEDURE Greet(Name : STRING, Age : INTEGER). Which call is correct?
A. CALL Greet("Sam", 15) ✓
B. CALL Greet(15, "Sam")
Values must match the parameters in order and in data type. The first value must be a STRING and the second an INTEGER.

Match the Pairs

FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION → This function has no RETURN statement, so it never sends a value back to the caller. Add RETURN N * 2.
CALL ShowTotal(5, 10) // header: PROCEDURE ShowTotal(A : INTEGER) → The call passes two values but the procedure has only one parameter. The number of values must match the parameters.
Every FUNCTION must be closed with ENDFUNCTION, and every PROCEDURE with ENDPROCEDURE → FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
A local variable only exists inside the subroutine that declares it, so the value must be returned or passed out → FUNCTION AddTwo(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result ← N + 2 RETURN Result ENDFUNCTION
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18 → The ENDFUNCTION keyword is missing, so the function is never closed. Add ENDFUNCTION on the last line.
PROCEDURE Total() DECLARE Sum : INTEGER Sum ← 5 + 3 ENDPROCEDURE OUTPUT Sum → Sum is a local variable declared inside the procedure, so it cannot be used in OUTPUT outside it. Output it inside the procedure.

Key Term

Debugging
Finding and fixing errors by checking a subroutine's header, parameter types, RETURN, CALL, end keyword and variable scope against the syntax rules.

Fill in the Blanks

1. FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword that closes a function definition.
2. FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Hint: Type the keyword that sends a value back to the caller. Without it the function gives nothing back.
3. PROCEDURE Add(A, B : INTEGER) CALL Add(5, 7)
Hint: Type the keyword used to run a procedure. Remember the number of values must match the parameters.
4. PROCEDURE ShowTotal() Total ← 10 + 5 OUTPUT Total
Hint: Type the local variable that is declared inside this procedure, so it can be used here.
5. FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age ≥ 18 ENDFUNCTION
Hint: Type the header keyword that comes before the data type of the value the function gives back.

Spot the Error

Question 1
A FUNCTION Six() INTEGER
B RETURN 6
C ENDFUNCTION
Line A — The function header is missing the RETURNS keyword. A function header must state the return type as RETURNS INTEGER.
Question 2
A PROCEDURE Greet(Name)
B OUTPUT "Hi ", Name
C ENDPROCEDURE
Line A — The parameter Name has no data type. Each parameter in the header needs a type, so it should be Name : STRING.
Question 3
A FUNCTION Rate() RETURNS REAL
B RETURN 0.5
C ENDPROCEDURE
Line C — A function must finish with ENDFUNCTION. ENDPROCEDURE is the wrong end keyword, so the function is never closed correctly.
Question 4
A FUNCTION Ten() RETURNS INTEGER
B Value <- 5
C Value <- Value * 2
D OUTPUT Value
E ENDFUNCTION
Line D — A function must send a value back with RETURN. OUTPUT only displays the value, so the function returns nothing. It should be RETURN Value.
Question 5
A FUNCTION Ok() RETURNS BOOLEAN
B RETURN "Yes"
C ENDFUNCTION
Line B — The function returns a BOOLEAN, but "Yes" is a STRING. The returned value must match the return type, so it should be RETURN TRUE.

Student with a class code? Join your class →