← IGCSE Computer Science Paper 2

Storing a function result in a variable

The function runs, and the value it returns is stored in the variable on the left of the arrow. You can use that variable later. Its type must match the function's return type.

Understanding Storing a function result in a variable in IGCSE Computer Science Paper 2

What it does: SumSquare returns an INTEGER, so Total is declared as INTEGER to match the return type. Each call runs the function with its own arguments and returns its own value, which is stored in its own variable.

Common mistakes: The arrow assigns the value on its right to the variable on its left. The variable must be on the left: Total ← SumSquare(3, 4). The variable that stores the returned value is Area. Aera is a misspelled name that was never assigned. It should be OUTPUT "Area is ", Area.

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 line stores the value returned by SumSquare in the variable Total?
SumSquare(3, 4) ← TotalTotal ← SumSquare(3, 4)
Matches
SumSquare(3, 4) ← Total
↓
The arrow points the wrong way. The variable must be on the left and the function call on the right, as in Total ← SumSquare(3, 4).
Applying
Meanings
Term
Total ← SumSquare(3, 4)
Meaning
The function runs, and the value it returns is stored in the variable on the left of the arrow. You can use that variable later. Its type must match the function's return type.
Practice
FUNCTION SumSquare(A : INTEGER, B : INTEGER) RETURNS INTEGER
Testing
Gaps
Total ← (3, 4)
Check Answer
Spots
1DECLARE Total : INTEGER
2SumSquare(3, 4) ← Total

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

The 'Penguin' badge — List Waddler, 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 line stores the value returned by SumSquare in the variable Total?
A. SumSquare(3, 4) ← Total
B. Total ← SumSquare(3, 4) ✓
The variable goes on the left of the arrow. The function call goes on the right. The returned value flows left into the variable.
2. SumSquare(A, B) returns A * A + B * B. After Total ← SumSquare(3, 4), what is stored in Total?
A. 25 ✓
B. 7
The function works out 3 * 3 + 4 * 4 = 25 and returns it. That returned value is what gets stored, not the arguments added together.
3. SumSquare is declared with RETURNS INTEGER. Which declaration suits the variable that stores its result?
A. DECLARE Total : INTEGER ✓
B. DECLARE Total : STRING
The variable type must match the function's return type. An INTEGER result needs an INTEGER variable.
4. After Total ← SumSquare(3, 4), what does OUTPUT Total display?
A. The returned value, 25 ✓
B. The text SumSquare(3, 4)
Total holds the value the function returned. It does not hold the function call itself, so OUTPUT shows 25.
5. After Total ← SumSquare(3, 4), how can you use the result again later in the program?
A. Call SumSquare(3, 4) again every time you need the value
B. Use the variable Total again without calling the function ✓
Storing the result in a variable lets you reuse it later. For example, Double ← Total * 2 does not need another function call.

Match the Pairs

Total ← SumSquare(3, 4) → Calls SumSquare with 3 and 4, then stores the value it returns in the variable Total.
OUTPUT Total → Uses the stored result later in the program by displaying the value held in Total.
The variable that stores a returned value must have the same type as the function's return type, so a function that returns INTEGER is stored in an INTEGER variable. → DECLARE Total : INTEGER Total ← SumSquare(3, 4)
Calling the same function twice with different values gives two results, which can be stored in two different variables. → First ← SumSquare(3, 4) Second ← SumSquare(5, 6)
SumSquare(3, 4) ← Total → The arrow points the wrong way. The variable must be on the left and the function call on the right, as in Total ← SumSquare(3, 4).
SumSquare(3, 4) OUTPUT Total → The returned value is never stored. Total has no value because the call is not assigned, so it should be Total ← SumSquare(3, 4).

Key Term

Total ← SumSquare(3, 4)
The function runs, and the value it returns is stored in the variable on the left of the arrow. You can use that variable later. Its type must match the function's return type.

Fill in the Blanks

1. Total ← SumSquare(3, 4)
Hint: Type the name of the function being called on the right of the arrow.
2. Total ← SumSquare(3, 4)
Hint: Type the assignment symbol that stores the returned value in the variable.
3. Total ← SumSquare(3, 4) OUTPUT Total
Hint: Type the variable that holds the stored result so it can be used later.
4. DECLARE Total : INTEGER Total ← SumSquare(3, 4)
Hint: SumSquare returns a whole number. Type the data type the variable needs to match.
5. Total1 ← SumSquare(3, 4) Total2 ← SumSquare(5, 6) OUTPUT Total1 + Total2
Hint: Type the function name to call it a second time with different values.

Spot the Error

Question 1
A DECLARE Total : INTEGER
B SumSquare(3, 4) ← Total
C OUTPUT Total
Line B — The arrow assigns the value on its right to the variable on its left. The variable must be on the left: Total ← SumSquare(3, 4).
Question 2
A DECLARE Area : INTEGER
B Area ← RectArea(5, 2)
C OUTPUT "Area is ", Aera
Line C — The variable that stores the returned value is Area. Aera is a misspelled name that was never assigned. It should be OUTPUT "Area is ", Area.
Question 3
A DECLARE Total : STRING
B Total ← SumSquare(3, 4)
C OUTPUT Total
Line A — SumSquare returns an INTEGER, so the variable that stores the result must be INTEGER too. It should be DECLARE Total : INTEGER.
Question 4
A DECLARE Ok : BOOLEAN
B Ok ← CALL IsEven(8)
C IF Ok = TRUE THEN
D OUTPUT "Even"
Line B — CALL is only used for procedures. A function is called by its name inside the expression: Ok ← IsEven(8).
Question 5
A DECLARE A, B : INTEGER
B A ← SumSquare(1)
C B ← SumSquare(3, 4)
D OUTPUT A + B
Line B — SumSquare needs two arguments, so the call must give two values. It should be A ← SumSquare(1, 2).

Student with a class code? Join your class →