Learning Objectives
- Distinguish local and global variables.
- Explain variable scope and its effect on maintainability.
- Use MOD, DIV, ROUND and RANDOM as library routines or operations.
- Select suitable library routines for a scenario.
Key Terms
- Local variable
- A variable declared inside a procedure or function and accessible only there.
- Global variable
- A variable declared outside subroutines and accessible to the wider program.
- Scope
- The region of code in which an identifier can be accessed.
- Library routine
- A predefined operation available for use without writing its internal algorithm.
- ROUND
- Returns a REAL rounded to a specified number of decimal places.
- RANDOM
- Returns a random number between 0 and 1 inclusive in Cambridge pseudocode.

Local Variables
A local variable is declared inside a procedure or function. It exists for that subroutine and cannot normally be accessed by the main program or another subroutine.
Local variables reduce unintended interaction. Two different procedures can each use a local variable called Count without sharing the same stored value.
Temporary calculations and loop variables used only by one subroutine should normally be local.
Global Variables
A global variable is declared outside procedures and functions and can be accessed more widely. It may be appropriate for data genuinely shared by many parts of a program.
However, global variables make it harder to know which statement changed a value. A procedure may depend on hidden global data rather than explicit parameters, reducing reuse and making testing harder.
Prefer parameters, return values and local variables where they express the data flow clearly. Use global data deliberately, not simply to avoid parameters.
Scope Example
TotalStudents can be read by the main program and procedure. Index belongs only to ShowClass. The comment labels are explanatory; the scope is actually determined by where the variables are declared.
DECLARE TotalStudents : INTEGER // global
PROCEDURE ShowClass
DECLARE Index : INTEGER // local
FOR Index <- 1 TO TotalStudents
OUTPUT Index
NEXT Index
ENDPROCEDURE
MOD And DIV
MOD and DIV are required arithmetic operations and are also listed among library routines in the syllabus guidance. MOD returns a remainder; DIV returns an integer quotient.
They support digit extraction, grouping and divisibility tests. Their operands should normally be integers.
ROUND
ROUND(Value, Places) returns Value rounded to the requested number of decimal places. Value should be REAL and Places should be INTEGER.
ROUND(12.3456, 2) returns 12.35. Rounding should normally occur when required for output or a specified calculation; avoid repeatedly rounding intermediate values unless instructed because precision may be lost.
RANDOM
RANDOM() returns a value from 0 to 1 inclusive according to the Cambridge pseudocode guide. It can be scaled and rounded to produce a required range.
For example, ROUND(RANDOM() * 6, 0) produces a whole value from 0 to 6 in the official illustration. When a question requires a different integer range, transform the random value carefully and state the possible endpoints.
Random results are unpredictable between runs, so test plans may need controlled or repeated execution rather than one expected fixed result.
Routine Summary
| Routine Or Operation | Example | Result |
|---|---|---|
| 17 DIV 5 | 17 DIV 5 | 3 |
| 17 MOD 5 | 17 MOD 5 | 2 |
| ROUND(8.376, 2) | ROUND(8.376, 2) | 8.38 |
| RANDOM() | RANDOM() | A value from 0 to 1 inclusive |
Worked Examples
Local Or Global
Question: A procedure uses TemporaryTotal only while calculating one report. Which scope is preferable?
- The value is not needed elsewhere.
- Limiting access prevents unrelated code from changing it.
Answer: Declare TemporaryTotal locally inside the procedure.
Rounded Average
Question: Round Average to one decimal place.
- Use Average as the REAL argument.
- Use 1 as the number of places.
- Store or output the returned value.
Answer: RoundedAverage <- ROUND(Average, 1).
Examination Guidance
- Define scope by where the variable is declared.
- Explain why local variables reduce unintended side effects.
- Use parameters rather than unnecessary globals.
- Give the correct purpose and arguments of ROUND.
- State the Cambridge RANDOM range when explaining it.
Common Mistakes
- Saying a local variable can be read anywhere.
- Treating all variables in a program as global.
- Rounding every intermediate calculation.
- Confusing MOD with DIV.
- Assuming RANDOM directly returns an integer from 1 to 6.
Knowledge Check
1. What is a local variable?
2. What is a global variable?
3. Why prefer local variables?
4. What does ROUND require?
5. What range does RANDOM return?