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-04.p
{ Advanced Pascal Programming Language Exercises
  Dr. Thomas W. MacFarland
  advanced_pencil-04.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_the_Use_of_Forward;

var X : integer;        {Global variable}

procedure Print(var Sum : integer); forward;

procedure Change(var Y : integer);
begin
   Y := Y - 1;
   if Y > 0 then
      Print(Y);
end;

procedure Print;
begin
   writeln('Sum = ',Sum:4);
   Change(Sum);
end;

begin  (* main program *)
   X := 3;
   Change(X);
   writeln;
   X := 2;
   Print(X);
end.  {advanced_pencil-04.p}
%  pc PASCAL/advanced_pencil-04.p
%  a.out
Sum =    2
Sum =    1

Sum =    2
Sum =    1

Please send comments or suggestions to Dr. Thomas W. MacFarland

There have been visitors to this page since February 1, 1999.