← IGCSE Computer Science Paper 2

Local compared with global variables

A local variable is declared inside a subroutine and only exists and works there while it runs. A global variable is declared outside and can be used anywhere in the program all the time.

Understanding Local compared with global variables in IGCSE Computer Science Paper 2

What it does: Inside Reset, Count means the local variable. Setting it to 0 does not change the global Count. Total is declared outside every subroutine, so ShowTotal can output it directly. A global variable has global scope, so every subroutine and the main program can use it. Each local variable belongs only to its own subroutine, so the same name can be reused safely.

Common mistakes: Num is a parameter, which is a local variable that only exists inside Show. The main program cannot use it. The call must pass a real value, such as CALL Show(7). Declaring Lives inside Reset creates a new local variable with the same name, which hides the global one. The global Lives is never set. Remove the DECLARE and assign Lives ← 3 so the global is changed.

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
A variable declared inside a subroutine is called...
A local variableA global variable
Matches
PROCEDURE Calc() DECLARE Temp : INTEGER Temp ← 5 ENDPROCEDURE CALL Calc() OUTPUT Temp
↓
Temp is a local variable of Calc, so it does not exist outside the procedure. Using it in the main program causes an error. Fix: OUTPUT Temp inside Calc, or use a function to RETURN the value.
Applying
Meanings
Term
local vs global
Meaning
A local variable is declared inside a subroutine and only exists and works there while it runs. A global variable is declared outside and can be used anywhere in the program all the time.
Practice
DECLARE Score : INTEGER
Testing
Gaps
Lives ← - 1
Check Answer
Spots
3ENDPROCEDURE
4CALL Show(Num)

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

The 'Blobfish' badge — Pressure Handler, 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 variable declared inside a subroutine is called...
A. A local variable ✓
B. A global variable
A local variable is declared inside a subroutine. A global variable is declared outside all subroutines.
2. Which variable can be used anywhere in the program?
A. A local variable
B. A global variable ✓
A global variable has global scope, so every subroutine and the main program can use it.
3. When does a local variable exist?
A. Only while its subroutine is running ✓
B. For the whole time the program runs
A local variable is created when the subroutine starts and is removed when it ends. A global variable lasts for the whole program.
4. The main program tries to OUTPUT a variable that was declared inside a procedure. What happens?
A. An error, because the local variable is out of scope ✓
B. It outputs the value the procedure gave it
A local variable cannot be used outside its subroutine, so the main program cannot see it.
5. Where is a global variable declared?
A. Inside one subroutine
B. Outside all subroutines ✓
Global variables are declared outside every subroutine, usually at the top of the program.

Match the Pairs

DECLARE Lives : INTEGER // outside all subroutines → A global variable: it can be used anywhere in the program and exists for the whole time the program runs.
DECLARE Temp : INTEGER // inside PROCEDURE Show() → A local variable: it only exists and can only be used inside Show while Show is running.
A local variable with the same name as a global variable is a separate variable, so the subroutine changes its own copy and the global keeps its value. → DECLARE Count : INTEGER PROCEDURE Reset() DECLARE Count : INTEGER Count ← 0 ENDPROCEDURE
A global variable can be used inside any subroutine without being passed in as a parameter. → DECLARE Total : INTEGER PROCEDURE ShowTotal() OUTPUT Total ENDPROCEDURE
PROCEDURE Calc() DECLARE Temp : INTEGER Temp ← 5 ENDPROCEDURE CALL Calc() OUTPUT Temp → Temp is a local variable of Calc, so it does not exist outside the procedure. Using it in the main program causes an error. Fix: OUTPUT Temp inside Calc, or use a function to RETURN the value.
PROCEDURE A() DECLARE X : INTEGER X ← 10 ENDPROCEDURE PROCEDURE B() OUTPUT X ENDPROCEDURE → X is local to procedure A, so procedure B cannot use it. Fix: declare X as a global variable, or pass it to B as a parameter.

Key Term

local vs global
A local variable is declared inside a subroutine and only exists and works there while it runs. A global variable is declared outside and can be used anywhere in the program all the time.

Fill in the Blanks

1. DECLARE Lives : INTEGER PROCEDURE LoseLife() Lives ← Lives - 1
Hint: Type the name of the global variable declared before the procedure, so it can be read inside it.
2. PROCEDURE Greet() DECLARE Count : INTEGER ENDPROCEDURE
Hint: Type the keyword that declares a variable. Declared inside the procedure, it is local.
3. // Global variable: Score PROCEDURE Reset() DECLARE Score : INTEGER // same name
Hint: Type the variable name that matches the global one, so the new local variable hides the global inside this procedure.
4. PROCEDURE Count() DECLARE Total : INTEGER ENDPROCEDURE // Total stops existing here
Hint: Type the keyword that ends the procedure, which is where the local variable stops existing.
5. DECLARE Limit : INTEGER FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N > Limit
Hint: Type the parameter name, which is local to the function, to compare it with the global variable.

Spot the Error

Question 1
A PROCEDURE Show(Num : INTEGER)
B OUTPUT Num
C ENDPROCEDURE
D CALL Show(Num)
Line D — Num is a parameter, which is a local variable that only exists inside Show. The main program cannot use it. The call must pass a real value, such as CALL Show(7).
Question 2
A DECLARE Lives : INTEGER
B PROCEDURE Reset()
C DECLARE Lives : INTEGER
D ENDPROCEDURE
E CALL Reset()
F OUTPUT Lives
Line C — Declaring Lives inside Reset creates a new local variable with the same name, which hides the global one. The global Lives is never set. Remove the DECLARE and assign Lives ← 3 so the global is changed.
Question 3
A DECLARE Total : INTEGER
B CALL Add(10)
C OUTPUT Num
D PROCEDURE Add(Num : INTEGER)
E Total ← Num
F ENDPROCEDURE
Line C — Num is a local parameter of Add and does not exist outside it, so the main program cannot output it. It should output the global variable Total, which can be used anywhere.
Question 4
A PROCEDURE Double()
B OUTPUT Num * 2
C ENDPROCEDURE
D CALL Double(4)
Line A — The parameter Num is a local variable that is created by the procedure header. Without Num in the header, the line OUTPUT Num * 2 uses a variable that is not in scope. The header should be PROCEDURE Double(Num : INTEGER).
Question 5
A DECLARE Pts : INTEGER
B PROCEDURE Add(Pts : INTEGER)
C Pts ← Pts + 5
D ENDPROCEDURE
E CALL Add()
F OUTPUT Pts
Line B — A parameter is a local variable. Naming it Pts hides the global Pts, so the procedure would change only the local copy and the global stays the same. The header should be PROCEDURE Add() so the global Pts is used.

Student with a class code? Join your class →