← IGCSE Computer Science Paper 2

Choosing the return data type of a function

Keyword in a function header, followed by a data type such as INTEGER, REAL, STRING or BOOLEAN. The value sent back by RETURN must be of that same type.

Understanding Choosing the return data type of a function in IGCSE Computer Science Paper 2

What it does: A count is always a whole number, so INTEGER is the type that matches the value sent back by RETURN. Text is stored as a STRING, so the header must say RETURNS STRING to match the text value returned. The type depends on the value being returned, not the parameter. A comparison gives TRUE or FALSE, so BOOLEAN is correct. The total is a number with decimal places, so it is REAL. STRING is for text.

Common mistakes: The function returns 15, which is a whole number, but the header says STRING. The header should say RETURNS INTEGER so it matches the value sent back. RETURN "Cost" sends back a STRING, but the header says RETURNS REAL. It should be RETURN Cost, which sends back the REAL value 4.5.

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 409–418) and word search (Modules 399–418).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A function calculates the average of three whole-number marks, which may include a decimal such as 72.5. Which header is best?
FUNCTION Average(a, b, c) RETURNS INTEGERFUNCTION Average(a, b, c) RETURNS REAL
Matches
FUNCTION IsPass(Mark : INTEGER) RETURNS INTEGER RETURN Mark >= 50 ENDFUNCTION
↓
The comparison Mark >= 50 gives TRUE or FALSE, which is a BOOLEAN, but the header says RETURNS INTEGER. It should be RETURNS BOOLEAN.
Applying
Meanings
Term
RETURNS
Meaning
Keyword in a function header, followed by a data type such as INTEGER, REAL, STRING or BOOLEAN. The value sent back by RETURN must be of that same type.
Practice
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
Testing
Gaps
FUNCTION Twice(N : INTEGER) RETURNS
Check Answer
Spots
1FUNCTION Age() RETURNS STRING
2 RETURN 15

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

The 'Roadrunner' badge — Streak Runner, 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 calculates the average of three whole-number marks, which may include a decimal such as 72.5. Which header is best?
A. FUNCTION Average(a, b, c) RETURNS REAL ✓
B. FUNCTION Average(a, b, c) RETURNS INTEGER
An average can have a decimal part, so the RETURN value must be REAL. INTEGER can only hold whole numbers.
2. A function checks whether a password is long enough and sends back TRUE or FALSE. Which data type follows RETURNS?
A. RETURNS BOOLEAN ✓
B. RETURNS STRING
TRUE and FALSE are BOOLEAN values, so the header must say RETURNS BOOLEAN.
3. Which statement about RETURNS is correct?
A. The type after RETURNS can be any type, whatever value RETURN sends back.
B. The type after RETURNS must match the type of the value sent back by RETURN. ✓
The data type in the header is a promise. The value in the RETURN statement must be of that same type.
4. A function builds a greeting such as "Hello Sam" and sends it back. Which header is correct?
A. FUNCTION Greet(Name) RETURNS STRING ✓
B. FUNCTION Greet(Name) RETURNS BOOLEAN
Text such as "Hello Sam" is a STRING, so the function must be declared RETURNS STRING.
5. FUNCTION CountItems() RETURNS INTEGER RETURN "ten" ENDFUNCTION What is wrong with this function?
A. Nothing is wrong, because RETURN can send back any type.
B. It returns a STRING but the header says INTEGER, so the types do not match. ✓
"ten" is a STRING. The header promises an INTEGER, so this is a type mismatch. Return a whole number such as 10.

Match the Pairs

FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN → The function sends back a BOOLEAN, so RETURN must give TRUE or FALSE.
FUNCTION AverageMark(Total : INTEGER) RETURNS REAL → The function sends back a REAL, so RETURN must give a number that can have a decimal part.
A function that returns a whole number such as a count must have RETURNS INTEGER in its header. → FUNCTION CountItems() RETURNS INTEGER
A function that returns a piece of text such as a name must have RETURNS STRING in its header. → FUNCTION GetName() RETURNS STRING
FUNCTION IsPass(Mark : INTEGER) RETURNS INTEGER RETURN Mark >= 50 ENDFUNCTION → The comparison Mark >= 50 gives TRUE or FALSE, which is a BOOLEAN, but the header says RETURNS INTEGER. It should be RETURNS BOOLEAN.
FUNCTION GetGreeting() RETURNS INTEGER RETURN "Hello" ENDFUNCTION → The function returns the text "Hello", which is a STRING, but the header says RETURNS INTEGER. It should be RETURNS STRING.

Key Term

RETURNS
Keyword in a function header, followed by a data type such as INTEGER, REAL, STRING or BOOLEAN. The value sent back by RETURN must be of that same type.

Fill in the Blanks

1. FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the data type that matches a whole number, because N * 2 gives a whole number when N is whole.
2. FUNCTION Half(N : INTEGER) RETURNS REAL RETURN N / 2 ENDFUNCTION
Hint: Type the data type for numbers with decimal places, because dividing with / can give a fractional result.
3. FUNCTION Grade(M : INTEGER) RETURNS STRING RETURN "Pass" ENDFUNCTION
Hint: Type the data type for text, because the RETURN line sends back a word in quotes.
4. FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN RETURN N MOD 2 = 0 ENDFUNCTION
Hint: Type the data type for a TRUE or FALSE value, because the expression after RETURN is a comparison.
5. FUNCTION NameLen(S : STRING) RETURNS INTEGER RETURN LENGTH(S) ENDFUNCTION
Hint: Type the data type that matches what LENGTH gives back (a count of characters), so the header does not return the wrong type.

Spot the Error

Question 1
A FUNCTION Age() RETURNS STRING
B RETURN 15
C ENDFUNCTION
D OUTPUT Age()
Line A — The function returns 15, which is a whole number, but the header says STRING. The header should say RETURNS INTEGER so it matches the value sent back.
Question 2
A FUNCTION Price() RETURNS REAL
B Cost ← 4.5
C RETURN "Cost"
D ENDFUNCTION
Line C — RETURN "Cost" sends back a STRING, but the header says RETURNS REAL. It should be RETURN Cost, which sends back the REAL value 4.5.
Question 3
A FUNCTION Name() RETURNS STRING
B DECLARE W : STRING
C W ← "Ava"
D RETURN LENGTH(W)
E ENDFUNCTION
Line D — LENGTH(W) gives an INTEGER, but the function is declared RETURNS STRING. It should be RETURN W so that a STRING is sent back.
Question 4
A FUNCTION Ok() RETURNS BOOLEAN
B Score ← 60
C RETURN Score
D ENDFUNCTION
Line C — Score is an INTEGER, but the header says RETURNS BOOLEAN. It should be RETURN Score >= 50, because a comparison gives TRUE or FALSE.
Question 5
A FUNCTION Avg() RETURNS INTEGER
B Total ← 7
C RETURN Total / 2
D ENDFUNCTION
Line A — 7 / 2 gives 3.5, which is a REAL value, so the header cannot say INTEGER. It should say RETURNS REAL.

Student with a class code? Join your class →