Skip to content
IGCSE·Tuition
Computer Science · Lessons

Explain local and global scope

A variable works perfectly in one part of your program and then seems to vanish in another.

On this page
  1. How do I tell local from global?
  2. Worked example
  3. Which mistake catches students most often?
  4. Self-check
  5. Where does this lead next?

Scope describes where in a program a variable can be used. A global variable belongs to the main program. A local variable belongs to one subroutine and stops existing when that subroutine ends.

This lesson sits in the programming structure module. It builds on choosing a data type and prepares you for building a subroutine. Exam questions ask you to state whether a variable is local or global, trace a program that uses both, or explain why local variables are good practice.

How do I tell local from global?

Look at where the variable is declared.

  • Declared in the main program, outside every subroutine: global. Every part of the program can read and change it.
  • Declared inside a subroutine: local. Only that subroutine can use it, and it is created fresh each time the subroutine is called.
  • A parameter in the subroutine’s brackets also behaves as a local variable for that subroutine.

Think of a whiteboard in a classroom (global) and a page in your own notebook (local). Anyone can write on the whiteboard. Only you use your notebook page, and a new page is started for each lesson.

Worked example

Trace this program and state the output.

DECLARE Total : INTEGER
Total ← 0

PROCEDURE AddPoints(Points : INTEGER)
    DECLARE Bonus : INTEGER
    Bonus ← 5
    Total ← Total + Points + Bonus
ENDPROCEDURE

CALL AddPoints(10)
CALL AddPoints(20)
OUTPUT Total

Here Total is global. Points and Bonus are local to AddPoints.

StepPointsBonusTotal
Startnot creatednot created0
CALL AddPoints(10)1050 + 10 + 5 = 15
CALL AddPoints(20)20515 + 20 + 5 = 40
After the callsgonegone40

The output is 40. Bonus is created again at each call, so the second call starts with a new Bonus of 5, not a leftover value. Total survives because it is global.

Which mistake catches students most often?

Mistaken answer: adding OUTPUT Bonus at the end of the program, after the two calls.

This is an error. Bonus is local to AddPoints and no longer exists once the procedure has finished. The main program cannot see it.

There are two correct ways to get a value out of a subroutine. Output it inside the subroutine, or have a function return it. Returning is covered in tracing a returned value.

A second mistake is thinking a local variable with the same name as a global changes the global. If a procedure declares its own Total, the global Total is untouched.

Self-check

1. In the example above, state which variables are local and which are global.

Show answer

Global: Total. Local to AddPoints: Points and Bonus.

2. State the output.

X ← 3

PROCEDURE Test()
    DECLARE X : INTEGER
    X ← 8
ENDPROCEDURE

CALL Test()
OUTPUT X
Show answer

The procedure declares its own local X, set to 8. The global X is never changed, so the main program outputs 3.

3. Give one reason why a programmer prefers local variables.

Show answer

A local variable cannot be changed by accident from elsewhere in the program, so errors are easier to find. Another valid reason: the same name can be reused in different subroutines without clashing.

Where does this lead next?

Scope tells you what a subroutine can see. Next, build a subroutine with clear parameters, so it receives what it needs instead of reaching into globals. Use the restricted pseudocode trace trainer to practise filling in the table above, then check the same logic in the safe Python reasoning sandbox.

If you want someone to watch your tracing and stop you at the first wrong row, that is what online one-to-one Computer Science tuition is for.

Questions people ask

What is the difference between a local and a global variable?

A global variable is declared in the main program and can be used throughout it, including inside subroutines. A local variable is declared inside a subroutine and exists only while that subroutine runs. Outside it, the name cannot be used. Check how your exam year's syllabus words this.

Why are local variables usually preferred?

A local variable cannot be changed by accident from elsewhere in the program, so a bug is easier to find. Two subroutines can also use the same name without clashing. Programs with fewer global variables are easier to read, test and reuse.

What happens if a local and a global variable have the same name?

Inside the subroutine the local variable is used, and the global one is left unchanged. They are two separate variables that happen to share a name. This is a common trace question, so write both values down separately.

Updated:

Your next step

If scope questions feel like guessing which variable survives, a one-to-one teacher can trace a short program with you and mark exactly where each variable is created and where it stops existing.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service