Understand local and global variable scope

Programming Concepts – Variable Scope (IGCSE 0478)

1. What is Scope?

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:

  • Avoid accidental modification of data.
  • Control memory use (globals are allocated once, locals each call).
  • Write clearer, easier‑to‑debug code.

2. Types of Scope

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

3. Declaration Syntax (Cambridge Pseudocode)

  • Global variable – placed before any routine.
    DECLARE Total : INTEGER
  • Local variable – placed at the very start of a routine (or block).
    PROCEDURE AddToTotal(Value : INTEGER)
        DECLARE Increment : INTEGER
        …
    ENDPROCEDURE
  • Parameter – appears in the routine header; it is automatically local.
    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.

4. Scope Rules – Summary Table

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

5. Complete Pseudocode Example (Global, Local, Parameter)

/* ---------- 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.
  • All references to Total inside AddToTotal affect the same global variable.

6. Common Mistakes (and how they are marked)

6.1 Undeclared‑Variable Error (AO2)
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.

6.2 Unintended Shadowing
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.

6.3 Forgetting that parameters are already local
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.

7. Best Practices for the IGCSE Exam

  • Declare a variable as close as possible to where it is first needed.
  • Prefer local variables for temporary data; keep globals to a minimum.
  • Use Pascal‑case (e.g. TotalScore) – identifiers are case‑insensitive, but consistent style aids marking.
  • When a global must be modified, simply refer to it by name – no special keyword is required in Cambridge pseudocode.
  • Always start a routine with all its DECLARE statements before any executable code.
  • Check for accidental shadowing: if a local variable has the same name as a global, the global becomes inaccessible inside that block.

8. Suggested Diagram (description for you to draw)

Variable scope diagram
Diagram description: A large rectangle labelled “Program”. At the top a shaded strip titled “Global Variables” contains 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.

9. Quick Revision Checklist

  1. Can you identify whether a variable is global, local or a parameter?
  2. Do you know the exact DECLARE syntax for each scope in Cambridge pseudocode?
  3. Can you explain why parameters are considered local to the routine?
  4. Are you aware that identifiers are case‑insensitive and that Pascal‑case is the recommended style?
  5. Can you spot a scope error such as an undeclared variable or unintended shadowing?
  6. Do you understand the memory and lifetime differences between global and local variables?

Create an account or Login to take a Quiz

45 views
0 improvement suggestions

Log in to suggest improvements to this note.