What is variable scope in Pascal ?
In programming, scope is the region in which a variable can exist, be accessed, and used. A variable can be of the following types:
Local Variables
The variables declared inside a subprogram are known as local variables. They are not accessible anywhere outside that subprogram.
Example
The example below shows the use of local variables. Variables have different values in the two procedures and the program because they are local. One procedure or program cannot impact the value of a local variable in another program, as we see here:
program Local;varx, y, z: integer;procedure local_one;varx, y, z: integer;begin(* local variables *)x := 3;y := 5;z := x + y;writeln('Procedure local_one');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);end;procedure local_two;varx, y, z: integer;begin(* local variables *)x := 6;y := 20;z := x * y;writeln('Procedure local_two');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);end;beginx := 50;y := 10;z := x - y;writeln('Program Local');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);local_one();local_two();end.
The above code gives the output below:
Program Local
value of x = 50 y = 10 and z = 40
Procedure local_one
value of x = 3 y = 5 and z = 8
Procedure local_two
value of x = 6 y = 20 and z = 120
Global Variables
A global variable is declared outside any procedure. It exists, can be accessed, and used anywhere in the program, e.g., inside any procedure, outside any procedure.
Example
The example below shows the use of global variables x, y and z throughout the program inside the two procedures and outside them:
program Global;varx, y, z: integer;procedure one;vara, b, c: integer;begin(* local variables *)a := 3;b := 5;c := a + b;writeln('Procedure one');writeln('Local Variables');writeln('value of a = ', a , ' b = ', b, ' and c = ', c);(* global variables *)x := 2;y := 6;z := x + y;writeln('Global Variables');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);end;procedure two;vara, b, c: integer;begin(* local variables *)a := 5;b := 8;c := a * b;writeln('Procedure two');writeln('Local Variables');writeln('value of a = ', a , ' b = ', b, ' and c = ', c);(* global variables *)x := 50;y := 30;z := x - y;writeln('Global Variables');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);end;beginx := 4;y := 10;z := x * y;writeln('Program Global');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);writeln();one();writeln();two();end.
The above code gives the output below:
Program Global
value of x = 4 y = 10 and z = 40
Procedure one
Local Variables
value of a = 3 b = 5 and c = 8
Global Variables
value of x = 2 y = 6 and z = 8
Procedure two
Local Variables
value of a = 5 b = 8 and c = 40
Global Variables
value of x = 50 y = 30 and z = 20
Example
The code below shows that local variables of the same name are given preference over global variables in a procedure. The display statement in line 15 shows garbage values because it is not showing the global variables. It is showing the local variables. Since the local variables have not been initialized yet, it shows garbage values before:
program Global;varx, y, z: integer;procedure local;varx, y, z: integer;beginwriteln('Procedure local');writeln('Global Variables');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);(* local variables *)x := 3;y := 5;z := x + y;writeln('Local Variables');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);{after the values of x, y and z have been changed, the procedure displays the values of these local variables instead of global}end;beginx := 4;y := 10;z := x * y;writeln('Program Global');writeln('value of x = ', x , ' y = ', y, ' and z = ', z);writeln();local();end.
The above code gives the output below:
Program Global
value of x = 4 y = 10 and z = 40
Procedure local
Global Variables
value of x = 0 y = -18440 and z = 0
Local Variables
value of x = 3 y = 5 and z = 8
Free Resources