← IGCSE Computer Science Paper 2

Local variables

Creates a local variable inside a procedure or function, e.g. DECLARE Length : INTEGER. It exists only while the subroutine runs and cannot be used in the main program.

Understanding Local variables in IGCSE Computer Science Paper 2

What it does: Result is declared with DECLARE inside the function body, so it is local to Double and exists only while Double runs. A local variable is created when the subroutine is called and stops existing when the subroutine ends. This is different from a global variable. Parameters are local to their subroutine, so Width cannot be used in the main program.

Common mistakes: A local variable is declared with DECLARE, a name, a colon and a data type. The colon is missing. It should be DECLARE Area : INTEGER. DIM is not the pseudocode keyword for declaring a local variable. Inside a procedure it must be DECLARE Count : INTEGER.

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
Which line creates a local variable inside a subroutine?
DECLARE Length : INTEGERLOCAL Length INTEGER
Matches
PROCEDURE Greet() OUTPUT Count ENDPROCEDURE DECLARE Count : INTEGER
↓
Count is declared outside the procedure in the main program, so it is not a local variable of Greet. DECLARE must be inside the procedure body.
Applying
Meanings
Term
DECLARE
Meaning
Creates a local variable inside a procedure or function, e.g. DECLARE Length : INTEGER. It exists only while the subroutine runs and cannot be used in the main program.
Practice
PROCEDURE CalcTotal(Price : INTEGER)
Testing
Gaps
Length : INTEGER
Check Answer
Spots
1PROCEDURE ShowArea()
2 DECLARE Area INTEGER

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

The 'Seal' badge — Smooth Formatter, 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 creates a local variable inside a subroutine?
A. LOCAL Length INTEGER
B. DECLARE Length : INTEGER ✓
In Cambridge pseudocode, a variable is created with DECLARE name : TYPE. Written inside a procedure or function body, it becomes a local variable.
2. A variable is declared inside a procedure. Where can it be used?
A. Only inside that procedure ✓
B. Anywhere in the whole program
A local variable can only be used inside the subroutine where it was declared. The main program cannot see it.
3. PROCEDURE ShowArea() DECLARE Area : INTEGER Area ← 20 ENDPROCEDURE CALL ShowArea() OUTPUT Area What is wrong with the main program?
A. Area should have been declared after ENDPROCEDURE
B. Area is local to ShowArea, so the main program cannot use it ✓
Area exists only inside ShowArea. Using it in the main program causes an error because it is not available there.
4. When does a local variable exist?
A. Only while the subroutine is running ✓
B. For the whole time the program is running
A local variable is created when the subroutine is called and stops existing when the subroutine ends. This is different from a global variable.
5. Is a parameter in a subroutine local or global?
A. Local to the subroutine ✓
B. Global to the whole program
Parameters are also local. For example, in PROCEDURE Greet(Name : STRING), Name can only be used inside Greet.

Match the Pairs

DECLARE Total : INTEGER → Creates a local variable called Total inside a subroutine, which can only be used in that subroutine
PROCEDURE ShowArea(Width : INTEGER) → Width is a parameter, which is also a local variable that can only be used inside ShowArea
A local variable is declared inside the body of a function, before it is used → FUNCTION Double(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result <- N * 2 RETURN Result ENDFUNCTION
A local variable is not available in the main program after the subroutine has finished → CALL ShowArea(5) OUTPUT Length
PROCEDURE Greet() OUTPUT Count ENDPROCEDURE DECLARE Count : INTEGER → Count is declared outside the procedure in the main program, so it is not a local variable of Greet. DECLARE must be inside the procedure body.
CALL Calculate() OUTPUT Length → Length is a local variable declared inside Calculate, so the main program cannot use it after the call.

Key Term

DECLARE
Creates a local variable inside a procedure or function, e.g. DECLARE Length : INTEGER. It exists only while the subroutine runs and cannot be used in the main program.

Fill in the Blanks

1. PROCEDURE ShowArea() DECLARE Length : INTEGER ENDPROCEDURE
Hint: Type the keyword that creates a local variable inside a subroutine
2. PROCEDURE ShowPrice() DECLARE Price : REAL Price ← 2.5
Hint: Type the data type used for numbers with decimal places
3. PROCEDURE ShowLength() DECLARE Length : INTEGER Length ← 5
Hint: Type the assignment symbol that stores a value in the local variable
4. PROCEDURE Show(Width : INTEGER) OUTPUT Width * 2 ENDPROCEDURE
Hint: A parameter is also local. Type its name to use it inside the procedure
5. PROCEDURE Show() DECLARE Mark : INTEGER Mark ← 80
Hint: Type the name of the local variable that is given a value on the next line

Spot the Error

Question 1
A PROCEDURE ShowArea()
B DECLARE Area INTEGER
C Area ← 5 * 4
D OUTPUT Area
E ENDPROCEDURE
Line B — A local variable is declared with DECLARE, a name, a colon and a data type. The colon is missing. It should be DECLARE Area : INTEGER.
Question 2
A PROCEDURE SetUp()
B DIM Count : INTEGER
C Count ← 0
D ENDPROCEDURE
Line B — DIM is not the pseudocode keyword for declaring a local variable. Inside a procedure it must be DECLARE Count : INTEGER.
Question 3
A PROCEDURE Hello()
B DECLARE Msg : STRING
C Msg = "Hi"
D OUTPUT Msg
E ENDPROCEDURE
Line C — Assignment in IGCSE pseudocode uses the arrow ←, not =. The line should be Msg ← "Hi".
Question 4
A PROCEDURE Show(N)
B DECLARE Temp : INTEGER
C Temp ← N * 2
D OUTPUT Temp
E ENDPROCEDURE
Line A — A parameter is a local variable too, so it needs a data type in the header. It should be PROCEDURE Show(N : INTEGER).
Question 5
A FUNCTION Age() RETURNS INTEGER
B DECLARE Years : INTEGER
C Yrs ← 15
D RETURN Years
E ENDFUNCTION
Line C — Yrs was never declared in this function, so it does not exist as a local variable. The line should use the declared name: Years ← 15.

Student with a class code? Join your class →