← IGCSE Computer Science Paper 2

Defining a function with parameters

A function header lists each parameter in brackets as name:TYPE, then RETURNS and the return type. The body uses the parameters in a RETURN expression and ends with ENDFUNCTION.

Understanding Defining a function with parameters in IGCSE Computer Science Paper 2

What it does: The single parameter Num is written as Num:INTEGER in the brackets, followed by RETURNS INTEGER. This function has three parameters, returns their total and is closed with ENDFUNCTION. The header uses RETURNS followed by the data type. RETURN (without the S) is the statement used inside the body. Every parameter needs its own data type, so each one is written as name:TYPE and they are separated by commas. A function must state what type of value it returns, so RETURNS and a type must follow the parameter list.

Common mistakes: A function must finish with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE is only used to close a procedure. The RETURN expression can only use the parameters in the header. C is not a parameter, so it should be RETURN A + B.

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
Which function header is written correctly?
FUNCTION SumSquare(INTEGER Number1, INTEGER Number2) RETURNS INTEGERFUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
Matches
FUNCTION SumSquare(Number1, Number2) RETURNS INTEGER
↓
The parameters have no data types. Each one must be written as name:TYPE, for example Number1:INTEGER.
Applying
Meanings
Term
FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
Meaning
A function header lists each parameter in brackets as name:TYPE, then RETURNS and the return type. The body uses the parameters in a RETURN expression and ends with ENDFUNCTION.
Practice
FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
Testing
Gaps
Double(N:INTEGER) RETURNS INTEGER
Check Answer
Spots
3 RETURN N * N
4ENDPROCEDURE

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

The 'Golden Phoenix' badge — Savannah Legend, 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 function header is written correctly?
A. FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER ✓
B. FUNCTION SumSquare(INTEGER Number1, INTEGER Number2) RETURNS INTEGER
Each parameter is written as name, colon, then data type: Number1:INTEGER. The type does not come first.
2. Where do the parameters of a function go?
A. In brackets straight after the function name ✓
B. After the word RETURNS
Parameters sit in brackets after the name. RETURNS comes after the brackets and is followed by the return data type.
3. Which header correctly shows the return type?
A. FUNCTION Double(Num:INTEGER) RETURN INTEGER
B. FUNCTION Double(Num:INTEGER) RETURNS INTEGER ✓
The header uses RETURNS followed by the data type. RETURN (without the S) is the statement used inside the body.
4. Which function body is correct?
A. RETURN Number1 * Number1 + Number2 * Number2 ENDFUNCTION ✓
B. RETURN Number1 * Number1 + Number2 * Number2 ENDPROCEDURE
A function is closed with ENDFUNCTION. ENDPROCEDURE is only used to close a procedure.
5. How should a function with two parameters separate them in the header?
A. Number1, Number2:INTEGER
B. Number1:INTEGER, Number2:INTEGER ✓
Every parameter needs its own data type, so each one is written as name:TYPE and they are separated by commas.

Match the Pairs

FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER → Header of a function with two INTEGER parameters that sends back an INTEGER result
RETURN Number1 * Number1 + Number2 * Number2 → Uses both parameters in the RETURN expression to calculate the value sent back
A function with one parameter: name, colon, data type in brackets, then RETURNS and the return type → FUNCTION Double(Num:INTEGER) RETURNS INTEGER
A function definition with parameters always finishes with ENDFUNCTION after the RETURN statement → FUNCTION AddThree(A:INTEGER, B:INTEGER, C:INTEGER) RETURNS INTEGER RETURN A + B + C ENDFUNCTION
FUNCTION SumSquare(Number1, Number2) RETURNS INTEGER → The parameters have no data types. Each one must be written as name:TYPE, for example Number1:INTEGER.
FUNCTION Average(A:INTEGER, B:INTEGER) RETURN (A + B) / 2 ENDFUNCTION → The header is missing RETURNS and the return data type after the brackets, for example RETURNS REAL.

Key Term

FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
A function header lists each parameter in brackets as name:TYPE, then RETURNS and the return type. The body uses the parameters in a RETURN expression and ends with ENDFUNCTION.

Fill in the Blanks

1. FUNCTION Double(N:INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword that starts a function definition.
2. FUNCTION Double(N:INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Hint: Type the keyword in the header that comes before the return data type.
3. FUNCTION Cube(N:INTEGER) RETURNS INTEGER RETURN N * N * N ENDFUNCTION
Hint: Type the keyword that closes the function definition.
4. FUNCTION Add(A:REAL, B:REAL) RETURNS REAL RETURN A + B ENDFUNCTION
Hint: Type the name of the second parameter so the RETURN expression uses both parameters.
5. FUNCTION IsOld(Age:INTEGER) RETURNS BOOLEAN RETURN Age >= 18 ENDFUNCTION
Hint: Type the return data type that matches a comparison, which gives True or False.

Spot the Error

Question 1
A FUNCTION Sq(N:INTEGER)
B RETURNS INTEGER
C RETURN N * N
D ENDPROCEDURE
Line D — A function must finish with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE is only used to close a procedure.
Question 2
A FUNCTION Add(A:REAL, B:REAL)
B RETURNS REAL
C RETURN A + C
D ENDFUNCTION
Line C — The RETURN expression can only use the parameters in the header. C is not a parameter, so it should be RETURN A + B.
Question 3
A FUNCTION Twice(INTEGER:N)
B RETURNS INTEGER
C RETURN N * 2
D ENDFUNCTION
Line A — Each parameter is written as identifier, colon, data type. The name comes first, so it should be (N:INTEGER).
Question 4
A FUNCTION Half(N:REAL)
B RETURNS
C RETURN N / 2
D ENDFUNCTION
Line B — RETURNS must be followed by the data type of the value the function gives back. It should be RETURNS REAL.
Question 5
A FUNCTION Area(L:REAL, W:REAL)
B RETURNS REAL
C OUTPUT L * W
D ENDFUNCTION
Line C — A function must send its answer back with RETURN. OUTPUT only displays a value and returns nothing. It should be RETURN L * W.

Student with a class code? Join your class →