The scope of a variable tells you which parts of the program it can be used in.

Local variables have a local scope: they can only be used inside the structure they're defined in.

Global variables have a global scope: they can be used anywhere.

Example 1 shows a program with both local and global variables.

Example 1
GLOBAL x = 0
GLOBAL y = 0

SUBROUTINE move(p, q)
  x ← x + p
  y ← y + q
ENDSUBROUTINE

move(2, 6)
move(4, 1)

OUTPUT x
OUTPUT y

The outputs of this program are 6 and 7.

x and y are global variables: they are used throughout the program.
p and q are local variables: they can only be used inside the move subroutine.



What are the advantages of only using local variables in a program?

It might save memory, because local variables are removed from memory after the subroutine that they are in has finished. It could also lead to less name clashes, where two variables are called the same thing, because two local variables can have the same name if they have different scopes.