Chapter 9
Answers to Exam Preparation Exercises
1. True. A function call is always a component of an expression, but a procedure call is always a statement in itself.
3. True. VAR parameters should generally not be used in a function.
6. Given the code segment
Sum := 0; (* Sum is of type Integer *)
WHILE NOT EOF(DataFile) DO
BEGIN
Readln(DataFile, Amount);
Sum := Sum + Round(Amount);
END;
Writeln(Sum:6);
and input data (each value on a separate line)
0.5 0.5 0.5 0.5 0.5 0.5 0.5 <EOF>
the output will be 7, even though the sum of the input values is 3.5. All of the 0.5 values are read correctly into the variable Amount, but in each case 1 is added to Sum because Round(0.5) = 1. If the function had been Trunc instead of Round, the final value of Sum would have been 0.
8. Procedure Power rewritten as a function:
FUNCTION Power (Base:
Real;
Exponent:
Integer):
Real;
VAR
Answer:
Real;
I:
Integer;
BEGIN (* Power *)
Answer := 1.0;
I := 1;
WHILE I <= Exponent DO
BEGIN
Answer := Answer * Base;
I := I + 1
END;
Power := Answer
END; (* Power *)
Function call:
M := Power(K, L, M)
9. Results of executing function Test.
a. –0.5
b. 0.5
10. Definitions. Several of these definitions are taken verbatim from the text.
recursion the action of a subprogram's calling itself.
result type the data type of the result value that is returned by a function.
representation error an arithmetic error that occurs because the precision of the true result of arithmetic operations is greater than the precision of the machine.
overflow the result of an arithmetic expression's being greater than the maximum number that can be represented on the computer.
mantissa the part of a real number that contains the representable significant digits.
exponent the part of a real number that determines where the decimal point is placed relative to the mantissa.
significant digits those digits from the first nonzero digit on the left to the last nonzero digit on the right (plus any zero digits that are exact).
11. The construct ": Boolean ;" should appear at the end of the parameter list. The "(" before the second parameter should be deleted. The data type of the second parameter should also be Boolean (although this does not cause a syntax error in Turbo Pascal, we recommend strongly against using the Boolean operators with the integer types).
Answers to Programming Warm-Up Exercises
1. Function heading:
FUNCTION Epsilon (High,
Low:
Real):
Real;
3. Function Equal (two versions):
FUNCTION Equal (Num1,
Num2,
Difference:
Real):
Boolean;
BEGIN (* Equal *)
IF Abs(Num1 - Num2) < Difference
THEN
Equal := True
ELSE
Equal := False
END; (* Equal *)
FUNCTION Equal (Num1,
Num2,
Difference:
Real):
Boolean;
BEGIN (* Equal *)
Equal := Abs(Num1 - Num2) < Difference
END; (* Equal *)
4. Function heading:
FUNCTION EOForEOLN:
Boolean;
8. Function P5 (two versions):
FUNCTION P5 (X:
Real):
Real;
BEGIN (* P5 *)
P5 := X * X * X * X * X
END; (* P5 *)
or
FUNCTION P5 (X:
Real):
Real;
BEGIN (* P5 *)
P5 := X * Sqr(Sqr(X))
END; (* P5 *)
9. Function Min (two versions):
FUNCTION Min (Number1,
Number2,
Number3:
Integer):
Integer;
VAR
Smallest:
Integer;
BEGIN (* Min *)
Smallest := Number1;
IF Number2 < Smallest
THEN
Smallest := Number2;
IF Number3 < Smallest
THEN
Smallest := Number3;
Min := Smallest
END; (* Min *)
or
FUNCTION Min (Number1,
Number2,
Number3:
Integer):
Integer;
BEGIN (* Min *)
IF Number1 < Number2
THEN
IF Number1 < Number3
THEN
Min := Number1
ELSE
Min := Number3
ELSE
IF Number2 < Number3
THEN
Min := Number2
ELSE
Min := Number3
END; (* Min *)
10. Boolean function to test whether an integer is prime.
FUNCTION TestForPrime (N: (* Number to test *)
Integer):
Boolean;
(* This function returns True if the value passed to *)
(* it is a prime number other than 1 *)
VAR
Limit, (* Greatest divisor to test *)
TrialDivisor: (* A trial divisor to test *)
Integer;
BEGIN (* TestForPrime *)
Limit := Trunc(Sqrt(N)) + 1; (* Only need to check *)
(* up to Sqrt(N) *)
TrialDivisor := 2;
TestForPrime := False; (* If N <= 1, it's not prime *)
IF N > 1
THEN (* Test divisors from 2 to Limit. If N *)
BEGIN (* = 2 or we reach Limit, N is a prime *)
WHILE (TrialDivisor <= Limit) AND
(N MOD TrialDivisor <> 0) DO
TrialDivisor := TrialDivisor + 1;
TestForPrime := (N = 2) OR (TrialDivisor = Limit)
END
END; (* TestForPrime *)
12. Recursive Function Power modified to allow zero as an exponent.
FUNCTION Power (X,
N:
Integer):
Integer;
BEGIN (* Power *)
IF N = 0
THEN
Power := 1
ELSE
Power := X * Power(X, N-1)
END; (* Power *)