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

{ The purpose of this program is to:

  1.  Demonstrate simple procedures that merely write a
      message to the screen.

  2.  Demonstrate how procedures can be included within
      another procedure.
}

program Procedure_Calling_Another_Procedure;

procedure A;
begin
   write('Begin A  ');
   write('A = 1    '); 
   writeln('End A  '); writeln;
end;

procedure B;
begin
   write('Begin B  ');
   A;
   write('B = 2    '); 
   writeln('End B  '); writeln;
end;

procedure C;
begin
   write('Begin C  ');
   A;
   B;
   write('C = 3    '); 
   writeln('End C  '); writeln;
end;

procedure D;
begin
   write('Begin D  ');
   A;
   B;
   C;
   write('D = 4    '); 
   writeln('End D  '); writeln;
end;

begin  (* main program *)
  A; writeln('***');
  B; writeln('***');
  C; writeln('***');
  D; writeln('***');
end.  {advanced_pencil-03.p}
%  pc PASCAL/advanced_pencil-03.p
%  a.out
Begin A  A = 1    End A  

***
Begin B  Begin A  A = 1    End A  

B = 2    End B  

***
Begin C  Begin A  A = 1    End A  

Begin B  Begin A  A = 1    End A  

B = 2    End B  

C = 3    End C  

***
Begin D  Begin A  A = 1    End A  

Begin B  Begin A  A = 1    End A  

B = 2    End B  

Begin C  Begin A  A = 1    End A  

Begin B  Begin A  A = 1    End A  

B = 2    End B  

C = 3    End C  

D = 4    End D  

***

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

There have been [an error occurred while processing this directive] visitors to this page since February 1, 1999.