← IGCSE Computer Science Paper 2

Why local variables are preferred

A variable declared inside a subroutine. It keeps the subroutine self-contained, so changing it cannot accidentally alter other parts of the program, and the subroutine is easier to test alone.

Understanding Why local variables are preferred in IGCSE Computer Science Paper 2

What it does: Each i is local to its own subroutine, so changing one never changes the other. Because Double needs nothing from the rest of the program, it can be called with a test value and checked by itself. Local variables with the same name in different subroutines are completely separate. You can reuse names like Total or Count safely. Parameters pass values in through the call, so the subroutine does not depend on global variables that other code might change. A variable declared inside a subroutine is local to it. A variable declared in the main program, outside any subroutine, is global.

Common mistakes: The header has no parameter, so N is never passed in. The procedure would have to rely on a global variable called N. It should be PROCEDURE Twice(N : INTEGER) so the value is passed in as a parameter. The parameter is called P, but the body uses Pts. Pts is not local to the procedure, so it would only work if a global called Pts existed. The parameter name must match the name used inside: Pts.

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
Why are local variables usually preferred over global variables?
They stop other parts of the program changing the value by mistakeThey make the program run faster on every computer
Matches
Score ← 0 (global) PROCEDURE Reset() Score ← 100 ENDPROCEDURE
↓
The procedure changes the global variable Score by mistake, which can break other parts of the program. Use a local variable instead.
Applying
Meanings
Term
local variable
Meaning
A variable declared inside a subroutine. It keeps the subroutine self-contained, so changing it cannot accidentally alter other parts of the program, and the subroutine is easier to test alone.
Practice
FUNCTION AddTax(Price : REAL) RETURNS REAL
Testing
Gaps
NewScore : INTEGER
Check Answer
Spots
1PROCEDURE Twice()
2 OUTPUT N * 2

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

The 'Woodpecker' badge — Tap Tap Tap, 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. Why are local variables usually preferred over global variables?
A. They stop other parts of the program changing the value by mistake ✓
B. They make the program run faster on every computer
A local variable only exists inside its subroutine, so no other code can change it accidentally. This keeps the subroutine self-contained.
2. Two procedures both declare a local variable called Total. What happens?
A. They share one Total, so changing it in one procedure changes it in the other
B. Each procedure has its own separate Total, so they do not affect each other ✓
Local variables with the same name in different subroutines are completely separate. You can reuse names like Total or Count safely.
3. Which is the better way to give a subroutine a value it needs?
A. Set a global variable before calling the subroutine
B. Pass the value in as a parameter ✓
Parameters pass values in through the call, so the subroutine does not depend on global variables that other code might change.
4. A procedure changes the global variable Score by mistake. What is the problem?
A. Other parts of the program that use Score may now get wrong values ✓
B. Nothing, because only that procedure can see Score
Global variables can be seen and changed anywhere. A mistake in one subroutine can cause errors in code that seems unrelated.
5. Which subroutine is easier to test on its own?
A. One that uses only its parameters and local variables ✓
B. One that reads and changes several global variables
If a subroutine does not depend on global variables, you can test it alone with chosen inputs and check the result.

Match the Pairs

DECLARE Total : INTEGER (inside PROCEDURE ShowTotal) → A local variable: it exists only inside this subroutine, so no other part of the program can change it by mistake
FUNCTION AddTax(Price : REAL) RETURNS REAL → The value is passed in as a parameter, so the function does not rely on a global variable and can be tested alone
Two subroutines can each use a local variable with the same name without affecting each other → PROCEDURE A() DECLARE i : INTEGER ... PROCEDURE B() DECLARE i : INTEGER
A subroutine that uses only local variables and parameters can be tested on its own → OUTPUT Double(5) // should output 10
Score ← 0 (global) PROCEDURE Reset() Score ← 100 ENDPROCEDURE → The procedure changes the global variable Score by mistake, which can break other parts of the program. Use a local variable instead.
FUNCTION Area() RETURNS INTEGER RETURN Width * Height ENDFUNCTION → The function depends on global variables Width and Height, so it is hard to test alone. Pass them in as parameters instead.

Key Term

local variable
A variable declared inside a subroutine. It keeps the subroutine self-contained, so changing it cannot accidentally alter other parts of the program, and the subroutine is easier to test alone.

Fill in the Blanks

1. PROCEDURE AddBonus(Score : INTEGER) DECLARE NewScore : INTEGER NewScore ← Score + 10
Hint: Type the keyword that creates a local variable inside the subroutine.
2. FUNCTION AddTax(Price : REAL) RETURNS REAL DECLARE Total : REAL Total ← Price * 1.2
Hint: Type the data type of the local variable that stores a price calculation.
3. PROCEDURE Reset() DECLARE Count : INTEGER Count ← 0
Hint: Type the name of the local variable declared on the line above.
4. FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Hint: Type the parameter name that passes the value in, so no global variable is needed.
5. // Test Square alone: expect 16 OUTPUT Square(4)
Hint: Type the keyword that displays a value so one self-contained subroutine can be tested on its own.

Spot the Error

Question 1
A PROCEDURE Twice()
B OUTPUT N * 2
C ENDPROCEDURE
Line A — The header has no parameter, so N is never passed in. The procedure would have to rely on a global variable called N. It should be PROCEDURE Twice(N : INTEGER) so the value is passed in as a parameter.
Question 2
A PROCEDURE Bonus(P : INTEGER)
B OUTPUT Pts + 10
C ENDPROCEDURE
Line A — The parameter is called P, but the body uses Pts. Pts is not local to the procedure, so it would only work if a global called Pts existed. The parameter name must match the name used inside: Pts.
Question 3
A PROCEDURE Calc(Num : INTEGER)
B DECLARE Temp : INTEGER
C Total ← Num * 2
D OUTPUT Temp
E ENDPROCEDURE
Line C — Total is not declared in this procedure, so this line would change a global variable by mistake. The procedure should store the result in its own local variable: Temp ← Num * 2.
Question 4
A PROCEDURE Add(N : INTEGER)
B DECLARE Sum : INTEGER
C Sum ← Total + 1
D OUTPUT Sum
E ENDPROCEDURE
Line C — Total is not a parameter or a local variable, so the procedure depends on a global variable outside it. That makes it hard to test alone. It should use the parameter: Sum ← N + 1.
Question 5
A FUNCTION Inc(N) RETURNS REAL
B DECLARE R : REAL
C R ← N + 1
D RETURN Total
E ENDFUNCTION
Line D — The function works out its answer in the local variable R, but returns Total, which is some other variable outside the function. It should be RETURN R.

Student with a class code? Join your class →