Chapter 7
Answers to Exam Preparation Exercises
1. Definitions:
positional matching the alignment between the formal parameters of a procedure and the actual parameters of a corresponding procedure call.
procedure call suspends execution of the current code and starts execution of the body of a procedure declaration.
actual parameter the value to be substituted for the formal parameter when a procedure is called.
parameter list the list of variables following the name of a procedure that determines how the procedure communicates with the code that called it.
VAR parameter a parameter through which the address of a variable is passed to a procedure so that a result may be passed back to the calling segment.
local variable a
variable that only can be used within the procedure in which it is declared and
cannot be seen or used outside the procedure.
formal parameter the name of a variable in the procedure heading that determines what the procedure will call a value.
3. Four advantages of using procedures and top-down design.
1) Design details can be encapsulated in procedures.
2) Top-down design modules correspond directly to procedures.
3) Individual modules can be separately tested and debugged.
4) Large, complex programs can be simplified.
5. Identifying items:
PROGRAM Fragment (Input, Output);
VAR
Variable1, Variable2, Variable3:
Boolean;
(* Procedure heading *)
PROCEDURE Test (VAR Param1, Param2, Param3: (* Formal parameters *)
Boolean);
VAR
Testl, (* Local variable *)
Test2: (* Local variable *)
Integer;
BEGIN (* Procedure body *)
.
.
END;
BEGIN
.
.
Test(Variable1, Variable3, Variable2); (* procedure call *)
(* ^----------^----------^----------------- actual parameters *)
Test(Variable2, Variable1, Variable3); (* procedure call *)
(* ^----------^----------^----------------- actual parameters *)
.
.
END.
6. Formal and actual parameters:
First Call to Test Second Call to Test
Formal Actual Formal Actual
1. Param1 Variable1 1. Param1 Variable2
2. Param2 Variable3 2. Param2 Variable1
3. Param3 Variable2 3. Param3 Variable3
8. Output from program Example:
In procedure Test, the variables equal 5 20
In the main program after the first call,
the variables equal 5 20
In Procedure Test, the variables equal 5 20
In the main program after the second call
the variables equal 5 20
10. Number the marked statements in the following program to show the order in which they are executed (the logical order of execution).
PROGRAM Execute (Input, Output)
VAR
Number1,
Number2:
Integer;
PROCEDURE Logical (VAR Value1,
Value2:
Integer);
VAR
Value3:
Integer;
BEGIN (* Logical *)
3 Readln(Value3, Value1);
4 Value2 := Value1 + 10
END; (* Logical *)
BEGIN (* Execute *)
1 Writeln('Exercise');
2 Logical(Number1, Number2);
5 Writeln(Number1:6, Number2:6)
END. (* Execute *)
12. If the last Writeln statement were changed to
Writeln(Value1:6, Value2:6)
the compiler would issue an error message saying that Value1 and Value2 were undefined variables. Because they are formal parameters to procedure Logical, they are not visible and do not exist outside of that procedure.
13. If data items 10 and 15 were read in by the program in exercise 9, the values of the variables at the end of the program would be Numberl = 15, Number2 = 25, Value3 = Undefined (local to the procedure only).
14. N will be decremented down to zero. So we have the sum of N numbers after the procedure finishes, but we do not know what N is unless we have copied it previously to another variable.
Answers to Programming Warm-Up Exercises
1. Max procedure heading:
PROCEDURE Max (VAR Number1, (* Receives parameter *)
Number2, (* Receives parameter *)
Greatest: (* Returns parameter *)
Integer);
2. Procedure completion:
PROCEDURE Halve (VAR First_Number, (* Receives/Returns *)
Second_Number: (* Receives/Returns *)
Integer);
BEGIN (* Halve *)
First_Number := First_Number DIV 2;
Second_Number := Second_Number DIV 2
END; (* Halve *)
5. Procedure Calculate_Fraction:
PROCEDURE Calculate_Fraction (VAR Answer:
Real);
BEGIN (* Calculate_Fraction *)
Answer := Answer - Trunc(Answer)
END; (* Calculate_Fraction *)
6. Procedure Find_Circum
PROCEDURE Find_Circum (VAR Radius,
Circumference:
Real);
CONST
Pi = 3.14159;
BEGIN (* Find_Circum *)
Circumference := 2 * Radius * Pi
END; (* Find_Circum *)
8. Procedure to count uppercase letters:
PROCEDURE Count_Upper(VAR Up_Count:
Integer);
VAR Ch:
Char;
BEGIN (* Count_Upper *)
Up_Count := 0;
WHILE NOT EOLN DO
BEGIN
Read(Ch);
IF (Ch >= 'A') AND (Ch <= 'Z')
THEN
Up_Count := Up_Count + 1
END;
END; (* Count_Upper *)
9. Modification of exercise 7 for four values (a complete program is shown):
PROGRAM RotateTest (Input, Output);
VAR
Value1,
Value2,
Value3,
Value4:
Integer;
PROCEDURE Rotate (VAR First_Value, (* Receives/Returns *)
Second_Value, (* Receives/Returns *)
Third_Value, (* Receives/Returns *)
Fourth_Value: (* Receives/Returns *)
Integer);
(* This procedure takes in four variables and returns *)
(* their values in a shifted order *)
VAR
Temp: (* Intermediate holding variable *)
Integer;
BEGIN (* Rotate *)
(* Save value in local variable *)
Temp := First_Value;
(* Shift next three variables *)
First_Value := Second_Value;
Second_Value := Third_Value;
Third_Value := Fourth_Value;
(* Replace final variable with saved value *)
Fourth_Value := Temp
END; (* Rotate *)
BEGIN (* RotateTest *)
Writeln('Enter four values');
Readln(Value1, Value2, Value3, Value4);
Writeln('Before: ', Value1, Value2, Value3, Value4);
Rotate (Value1, Value2, Value3, Value4);
Writeln('After: ', Value1, Value2, Value3, Value4)
END. (* RotateTest *)
10. Procedure that skips blanks on Input:
PROCEDURE Skip_Blanks (VAR Ch:
Char);
BEGIN (* Skip_Blanks *)
(* Initialize to blank to ensure loop will start *)
Ch := ' ';
WHILE Ch = ' ' DO
IF EOLN
THEN
Readln
ELSE
Read(Ch)
END; (* Skip_Blanks *)
11. Procedure that skips nonblanks:
PROCEDURE Skip_To_Blank (VAR Ch:
Char);
BEGIN (* Skip_To_Blank *)
(* Initialize to nonblank to ensure loop will start *)
Ch := '*';
WHILE Ch <> ' ' DO
IF EOLN
THEN
Readln
ELSE
Read(Ch)
END; (* Skip_To_Blank *)
13. Procedure that skips to blank and records number of characters skipped:
PROCEDURE Skip_To_Blank (VAR Ch:
Char;
VAR Skipped:
Integer);
BEGIN (* Skip_To_Blank *)
(* If first character read is nonblank, none have been
skipped *)
Skipped := -1;
Ch := '*';
WHILE Ch <> ' ' DO
IF EOLN
THEN
Readln
ELSE
BEGIN
Skipped := Skipped + 1;
Read(Ch)
END
END; (* Skip_To_Blank *)