Scope defines the part of a program where a variable’s name is recognised and its value can be read or changed. Understanding scope helps you:
| Scope | Where declared | Visibility | Lifetime |
|---|---|---|---|
| Global | Outside any PROCEDURE or FUNCTION |
All routines in the program can read/write (unless shadowed) | From start of program until termination |
| Local | Inside a routine or a block (e.g. IF, WHILE) |
Only the routine or block where it is declared | Only while that routine/block is executing |
| Parameter | In the routine header | Only the routine that receives the parameter | Only while that routine is executing |
DECLARE Total : INTEGER
PROCEDURE AddToTotal(Value : INTEGER)
DECLARE Increment : INTEGER
…
ENDPROCEDURE
PROCEDURE AddToTotal(Value : INTEGER)
Identifiers are case‑insensitive (e.g. Total, total, TOTAL are the same). The syllabus recommends Pascal‑case (first letter capital, subsequent words capitalised) for readability.
| Aspect | Global Variable | Local Variable | Parameter |
|---|---|---|---|
| Where declared | Outside any routine | Inside a routine or block | In the routine header |
| Lifetime | From start of program to termination | Only while the routine (or block) is executing | Only while the routine is executing |
| Visibility | All routines can read/write (unless shadowed) | Only the routine/block where declared | Only the routine where declared |
| Memory allocation | Allocated once at program start | Allocated each time the routine is entered | Allocated each time the routine is entered |
| Typical use | Configuration constants, shared counters | Temporary calculations, loop counters | Values passed into a routine |
/* ---------- Global variable ---------- */
DECLARE Total : INTEGER
Total ← 0
/* ---------- Procedure that adds a value to the global total ---------- */
PROCEDURE AddToTotal(Value : INTEGER)
/* Value – parameter (local to this procedure) */
DECLARE Increment : INTEGER
Increment ← Value /* local variable */
Total ← Total + Increment /* uses the global variable */
OUTPUT "Inside AddToTotal, Total = ", Total
ENDPROCEDURE
/* ---------- Main routine ---------- */
PROCEDURE Main()
OUTPUT "Before calling AddToTotal, Total = ", Total
CALL AddToTotal(5)
OUTPUT "After calling AddToTotal, Total = ", Total
ENDPROCEDURE
CALL Main()
Key points to note
Total is declared before any routine → global.Value appears in the header → parameter (automatically local).Increment is declared inside the procedure → local variable.Total inside AddToTotal affect the same global variable.PROCEDURE Wrong()
Counter ← Counter + 1 /* Counter has never been DECLARE‑d */
ENDPROCEDURE
Examiners award a scope error mark because the variable is used before it is declared.
DECLARE Total : INTEGER
Total ← 10
PROCEDURE Shadow()
DECLARE Total : INTEGER /* New local variable hides the global one */
Total ← 5
OUTPUT Total /* Prints 5 – the global Total is unchanged */
ENDPROCEDURE
Remember: the local Total exists only inside Shadow. The global Total remains 10.
DECLARE Sum : INTEGER
Sum ← 0
PROCEDURE Add(Value : INTEGER)
Sum ← Sum + Value /* Uses the global Sum – OK */
ENDPROCEDURE
PROCEDURE Wrong(Value : INTEGER)
DECLARE Sum : INTEGER /* New local Sum hides the global one */
Sum ← Sum + Value /* Uses the uninitialised local Sum – error */
ENDPROCEDURE
Never redeclare a name that already appears as a parameter unless you deliberately want a new local variable.
TotalScore) – identifiers are case‑insensitive, but consistent style aids marking.DECLARE statements before any executable code.
Total. Below are two smaller rectangles labelled “Procedure AddToTotal” and “Procedure Main”. Inside each rectangle is a shaded area “Local Variables”. The “AddToTotal” box also shows a labelled arrow “Parameter: Value”. Arrows from the global strip point to each procedure, illustrating that both can read/write the global variable, while the parameter arrow points only into AddToTotal.
DECLARE syntax for each scope in Cambridge pseudocode?Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.