← IGCSE Computer Science Paper 2

Function

A named subroutine that returns a single value to the place where it is called, so the call can be used inside an expression, such as x ← Total(2, 3).

Understanding Function in IGCSE Computer Science Paper 2

What it does: Double(4) is replaced by the value it returns (8), so it can be added to 1 like any other value. RETURNS BOOLEAN states that the single value coming back will be TRUE or FALSE. A function always returns one value. A procedure does not return a value. A function call produces a value, so it can be stored in a variable with an assignment. CALL is used for procedures. The call is replaced by the returned value 5 in the expression, so 5 + 1 = 6 is output.

Common mistakes: The header must say RETURNS before the data type of the value that comes back. It should be FUNCTION Pi() RETURNS REAL. A function must send its value back to the call with RETURN. OUTPUT only displays it and nothing comes back. It should be RETURN Result.

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 389–398) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which statement describes a function?
A named subroutine that returns a single value to where it is calledA named subroutine that never gives anything back to the program
Matches
FUNCTION Double(N : INTEGER) RETURN N * 2 ENDFUNCTION
↓
The header has no RETURNS data type, so the type of the value that comes back is not stated. It should be RETURNS INTEGER.
Applying
Meanings
Term
FUNCTION
Meaning
A named subroutine that returns a single value to the place where it is called, so the call can be used inside an expression, such as x ← Total(2, 3).
Practice
FUNCTION Total(A : INTEGER, B : INTEGER) RETURNS INTEGER
Testing
Gaps
Twice(N : INTEGER) RETURNS INTEGER
Check Answer
Spots
1FUNCTION Pi() REAL
2 RETURN 3.14

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

The 'Manta Ray' badge — Graceful Coder, 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. Which statement describes a function?
A. A named subroutine that returns a single value to where it is called ✓
B. A named subroutine that never gives anything back to the program
A function always returns one value. A procedure does not return a value.
2. Which line is a correct way to use a function call?
A. CALL Total(2, 3) ← Answer
B. Answer ← Total(2, 3) ✓
A function call produces a value, so it can be stored in a variable with an assignment. CALL is used for procedures.
3. How many values does a function return to the place where it is called?
A. One value ✓
B. Several values at once
A function returns a single value. That value then replaces the function call in the expression.
4. A function is declared like this: FUNCTION Area(W : INTEGER) RETURNS INTEGER. What does RETURNS INTEGER tell us?
A. The parameter W must be typed in by the user
B. The value returned has the data type INTEGER ✓
The data type after RETURNS is the type of the value that comes back from the function.
5. Which is valid use of a function call inside an expression?
A. Score ← Total(2, 3) + 10 ✓
B. Score ← CALL Total(2, 3) + 10
A function call can be part of an expression because it gives back a value. The word CALL is not used with functions.

Match the Pairs

FUNCTION Double(N : INTEGER) RETURNS INTEGER → Starts a function called Double that takes an integer and sends back one INTEGER value
RETURN N * 2 → Sends one value back to the place where the function was called
A function call can be used as part of an expression because it gives back one value → Answer ← Double(4) + 1
The returned value has a data type that is declared in the function header → FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
FUNCTION Double(N : INTEGER) RETURN N * 2 ENDFUNCTION → The header has no RETURNS data type, so the type of the value that comes back is not stated. It should be RETURNS INTEGER.
FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION → The function uses OUTPUT instead of RETURN, so no value comes back to the call. It should be RETURN N * 2.

Key Term

FUNCTION
A named subroutine that returns a single value to the place where it is called, so the call can be used inside an expression, such as x ← Total(2, 3).

Fill in the Blanks

1. FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword that starts the definition of a subroutine that gives back a value.
2. FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword that comes before the data type of the value the function sends back.
3. FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword that sends the single value back to the place where the function was called.
4. FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N > 100 ENDFUNCTION
Hint: Type the data type of the value returned. The comparison gives TRUE or FALSE.
5. x ← Total(2, 3) OUTPUT x
Hint: Type the assignment symbol that stores the value returned by the function call in the variable.

Spot the Error

Question 1
A FUNCTION Pi() REAL
B RETURN 3.14
C ENDFUNCTION
D OUTPUT Pi()
Line A — The header must say RETURNS before the data type of the value that comes back. It should be FUNCTION Pi() RETURNS REAL.
Question 2
A FUNCTION Half(N) RETURNS REAL
B Result ← N / 2
C OUTPUT Result
D ENDFUNCTION
Line C — A function must send its value back to the call with RETURN. OUTPUT only displays it and nothing comes back. It should be RETURN Result.
Question 3
A FUNCTION Sum(A,B) RETURNS REAL
B Result ← A + B
C RETURN Result
D ENDPROCEDURE
E OUTPUT Sum(2,3)
Line D — A function definition must be closed with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE belongs to procedures.
Question 4
A FUNCTION Sq(N) RETURNS REAL
B RETURN N * N
C ENDFUNCTION
D Area ← CALL Sq(5)
Line D — CALL is only used for procedures. A function is called by its name inside an expression, so it should be Area ← Sq(5).
Question 5
A FUNCTION Hi() RETURNS STRING
B RETURN 42
C ENDFUNCTION
D OUTPUT Hi()
Line B — The function is declared to return a STRING, but 42 is an INTEGER. The returned value must match the declared type, e.g. RETURN "Hello".

Student with a class code? Join your class →