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-07.p
{ Advanced Pascal Programming Language Exercises
Dr. Thomas W. MacFarland
advanced_pencil-07.p
}
{ The purpose of this program is to:
1. Demonstrate the use of a user-created function.
2. Demonstrate simple math operations within a function.
}
program Demonstrate_a_User_Created_Function;
var A, B, C, Sum : integer;
function Total(X, Y, Z : integer) : integer;
begin
Total := 3 * (X - Y + (2*Z));
end; {end function Total}
begin (* main program *)
A := 8;
B := 6;
C := 2;
Sum := Total(A,B,C);
writeln('A = ', A:8);
writeln('B = ', B:8);
writeln('C = ', C:8);
writeln('Computed value = ',Sum:8);
end. {advanced_pencil-07.p}
% pc PASCAL/advanced_pencil-07.p
% a.out
A = 8
B = 6
C = 2
Computed value = 18