← IGCSE Computer Science Paper 2

Global variables

A variable declared outside every subroutine, so the main program and any procedure or function can use and change it. It exists for the whole program.

Understanding Global variables in IGCSE Computer Science Paper 2

What it does: The function uses the global variable Score directly, without a parameter, because a global variable can be used in any subroutine. Global variables exist for the whole program. Local variables only exist while their subroutine runs.

Common mistakes: Assignment in IGCSE pseudocode uses the ← arrow, not =. The global variable Score should be set with Score ← 0. AddTen changes the global variable Total but returns nothing, so it is a procedure. The line should be PROCEDURE AddTen() to match ENDPROCEDURE.

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
Where is a global variable declared?
Outside every subroutine, at the start of the programInside one procedure only
Matches
PROCEDURE SetUp() DECLARE Lives : INTEGER Lives ← 3 ENDPROCEDURE OUTPUT Lives
↓
Lives is declared inside the procedure, so it is a local variable. The main program cannot use it. To make it global, declare it at the start of the program, outside the procedure.
Applying
Meanings
Term
global variable
Meaning
A variable declared outside every subroutine, so the main program and any procedure or function can use and change it. It exists for the whole program.
Practice
DECLARE Score : INTEGER
Testing
Gaps
Score : INTEGER
Check Answer
Spots
1DECLARE Score : INTEGER
2Score = 0

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

The 'Dumbo Octopus' badge — Module Maker, 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. Where is a global variable declared?
A. Inside one procedure only
B. Outside every subroutine, at the start of the program ✓
A global variable is declared outside every subroutine, usually with DECLARE at the start of the program. A variable declared inside a procedure is local.
2. Which line declares a global variable in IGCSE pseudocode?
A. DECLARE Score : INTEGER (before any PROCEDURE) ✓
B. GLOBAL Score = 0 inside a FUNCTION
Cambridge pseudocode uses DECLARE name : TYPE. Placed outside all subroutines, it makes the variable global.
3. Which parts of a program can use a global variable?
A. Only the main program
B. The main program and every procedure and function ✓
A global variable is available everywhere in the program, including inside any subroutine.
4. A global variable Total is declared at the start. A procedure runs Total ← Total + 5. What happens to the global Total?
A. Its value changes for the whole program ✓
B. Only a temporary copy changes inside the procedure
A subroutine can change a global variable directly. The new value stays after the subroutine ends.
5. How long does a global variable exist?
A. For the whole time the program runs ✓
B. Only while one subroutine is running
Global variables exist for the whole program. Local variables only exist while their subroutine runs.

Match the Pairs

DECLARE Score : INTEGER → When written at the start of the program, outside every subroutine, this creates a global variable that the whole program can use
Score ← Score + 10 → Inside a procedure, this changes the global variable Score, and the new value is still there after the procedure ends
A global variable is available everywhere, so a function can use it without it being passed in as a parameter → FUNCTION DoubleScore() RETURNS INTEGER RETURN Score * 2 ENDFUNCTION
A global variable exists for the whole program, so the main program can output it after a procedure has changed it → CALL AddPoints() OUTPUT Score
PROCEDURE SetUp() DECLARE Lives : INTEGER Lives ← 3 ENDPROCEDURE OUTPUT Lives → Lives is declared inside the procedure, so it is a local variable. The main program cannot use it. To make it global, declare it at the start of the program, outside the procedure.
PROCEDURE AddPoints() Score ← Score + 5 ENDPROCEDURE DECLARE Score : INTEGER → Score is declared after the procedure, not at the start of the program. The global DECLARE should come first, before the subroutines and the main program use it.

Key Term

global variable
A variable declared outside every subroutine, so the main program and any procedure or function can use and change it. It exists for the whole program.

Fill in the Blanks

1. DECLARE Score : INTEGER
Hint: Type the keyword that creates a variable at the start of the program, outside every subroutine.
2. DECLARE Score : INTEGER Score ← 0
Hint: Type the data type used for whole numbers.
3. PROCEDURE AddPoint() Score ← Score + 1 ENDPROCEDURE
Hint: Type the name of the global variable that the procedure changes.
4. FUNCTION DoubleScore() RETURNS INTEGER RETURN Score * 2 ENDFUNCTION
Hint: Type the name of the global variable that the function reads even though it was declared outside.
5. DECLARE Score : INTEGER Score ← 0 CALL AddPoint()
Hint: Type the keyword the main program uses to run a procedure.

Spot the Error

Question 1
A DECLARE Score : INTEGER
B Score = 0
C PROCEDURE AddPoint()
D Score ← Score + 1
E ENDPROCEDURE
Line B — Assignment in IGCSE pseudocode uses the ← arrow, not =. The global variable Score should be set with Score ← 0.
Question 2
A DECLARE Total : INTEGER
B Total ← 0
C FUNCTION AddTen()
D Total ← Total + 10
E ENDPROCEDURE
Line C — AddTen changes the global variable Total but returns nothing, so it is a procedure. The line should be PROCEDURE AddTen() to match ENDPROCEDURE.
Question 3
A DECLARE Coins : INTEGER
B PROCEDURE Spend()
C Coin ← Coins - 1
D ENDPROCEDURE
E Coins ← 5
F CALL Spend()
Line C — The global variable is called Coins, not Coin. The procedure must use the exact declared name: Coins ← Coins - 1.
Question 4
A DECLARE Name = STRING
B Name ← "Sam"
C PROCEDURE Greet()
D OUTPUT "Hi ", Name
E ENDPROCEDURE
Line A — A DECLARE statement gives the data type after a colon. It should be DECLARE Name : STRING.
Question 5
A DECLARE Level : INTEGER
B Level ← "1"
C PROCEDURE NextLevel()
D Level ← Level + 1
E ENDPROCEDURE
Line B — Level was declared as an INTEGER, so it cannot store the string "1". It should be Level ← 1.

Student with a class code? Join your class →