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

{ The purpose of this program is to:

  1.  Demonstrate one-way data movement in a procedure. 

  2.  Demonstrate two-way data movement in a procedure, 
      using var.
}

program Demonstrate_One_Way_and_Two_Way_Data_in_a_Procedure;

var A,B,C,D : integer;        {Global variables}

procedure Add_The_Sum(W,X,Y   : integer;  (* one-way *)
                      var Z   : integer); (* two-way *)
begin {procedure}
   W  := 2;
   X  := 8;
   Y  := 1;
   Z  := W + X + Y;
   writeln('Inside the procedure .... ');
   write('W   = ', W); writeln('  X   = ', X);
   write('Y   = ', Y); writeln('  Sum = ', Z);
end; {procedure}

begin  (* main program *)

   writeln('Outside the procedure ... ');
   A := 1; B := 2; C := 3; D := 4;
   write('A   = ', A); writeln('  B   = ', B);
   write('C   = ', C); writeln('  Sum = ', D);

   Add_The_Sum(A,B,C,D);

   writeln('Outside the procedure ... ');
   A := 4; B := 3; C := 2; D := 1;
   write('A   = ', A); writeln('  B   = ', B);
   write('C   = ', C); writeln('  Sum = ', D);

   Add_The_Sum(A,B,C,D);

end.  {advanced_pencil-05.p}
%  pc PASCAL/advanced_pencil-05.p
%  a.out
Outside the procedure ... 
A   =          1  B   =          2
C   =          3  Sum =          4
Inside the procedure .... 
W   =          2  X   =          8
Y   =          1  Sum =         11
Outside the procedure ... 
A   =          4  B   =          3
C   =          2  Sum =          1
Inside the procedure .... 
W   =          2  X   =          8
Y   =          1  Sum =         11

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

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