Advanced Pascal Programming Language Problems
© 1998 by Dr. Thomas W. MacFarland -- All Rights Reserved
Instructions: Review the following program and see if you can predict the outcome by completing a "pencil trace" of the code. Program output is provided at the end of this page. Compiler: The program was prepared using Standard Pascal on a UNIX-based host computer.
% cat PASCAL/advanced_pencil-01.p
{ Advanced Pascal Programming Language Exercises
Dr. Thomas W. MacFarland
advanced_pencil-01.p
}
{ The purpose of this program is to:
1. Demonstrate simple procedures that merely write a
message to the screen.
2. Demonstrate how a procedure can use a non-local variable
(i.e., a global variable that is available to all
procedures).
}
program Demonstrate_a_Simple_Procedure_Call;
var Count : integer; {Global variable}
{-------------------------------------------------------------------}
procedure Write_a_Message;
begin
writeln('Here is the message: Hello.');
end; {procedure Write_a_Message}
{-------------------------------------------------------------------}
{-------------------------------------------------------------------}
procedure Put_in_a_Count_1;
begin
writeln('And the count is: ',Count:4);
end; {procedure Put_in_a_Count_1}
{-------------------------------------------------------------------}
{-------------------------------------------------------------------}
procedure Put_in_a_Count_2;
begin
writeln('And the count is: ',Count:4);
end; {procedure Put_in_a_Count_2}
{-------------------------------------------------------------------}
{-------------------------------------------------------------------}
procedure Write_the_End;
begin
writeln('And you are now at the end.');
end; {procedure Write_the_End}
{-------------------------------------------------------------------}
{-------------------------------------------------------------------}
begin (* main program *)
Write_a_Message;
Count := 1;
for Count := Count to 4 do
Put_in_a_Count_1;
Count := 2;
for Count := Count to 6 do
Put_in_a_Count_2;
Write_the_End;
end. { advanced_pencil-01.p}
{-------------------------------------------------------------------}
% pc PASCAL/advanced_pencil-01.p
% a.out
Here is the message: Hello.
And the count is: 1
And the count is: 2
And the count is: 3
And the count is: 4
And the count is: 2
And the count is: 3
And the count is: 4
And the count is: 5
And the count is: 6
And you are now at the end.