PROCEDURES AND LOCAL VARIABLES
A procedure can declare it's own variables to work with. These variables belong to the procedure in which they are declared. Variables declared inside a procedure are known as local.
Local variables can be accessed anywhere between the begin and matching end keywords of the procedure. The following program illustrates the use and scope (where variables are visible or known) of local variables.
program LOCAL_VARIABLES (input, output);
var number1, number2 : integer; {these are accessible by all}procedure add_numbers;
var result : integer; {result belongs to add_numbers}
begin
result := number1 + number2;
writeln('Answer is ',result)
end;begin {program starts here}
writeln('Please enter two numbers to add together');
readln( number1, number2 );
add_numbers
end.
=====
Determine this programs output.
program MUSIC (output);
const SCALE = 'The note is ';
var JohnnyOneNote : char;procedure Tune;
const SCALE = 'The note now is ';
var JohnnyOneNote : char;
begin
JohnnyOneNote := 'A';
writeln(SCALE, JohnnyOneNote )
end;begin
JohnnyOneNote := 'D';
writeln(SCALE, JohnnyOneNote );
Tune;
writeln(SCALE, JohnnyOneNote )
end.Self Test on Local variables, output of program MUSIC is,
The note is D
The note now is A
The note is D