As you have seen, it is possible to declare a function as part of a program. It
is certainly permitted to declare a function like this, and doing so provides
an easy way to test the function. However, the real usefulness of functions,
and of procedures for that matter, is achieved when they are collected together
as a group of related items and placed in a package. A package is compiled and
placed in a library, either your own personal program library or, in a group
project, the team's library. Once a package is compiled, it--and all the
resources in it--are available for use by means of a simple context clause
(WITH
statement).
Recall from Section 3.7 that a package consists of two files: the specification and the body. The specification is like a "table of contents" for the package, listing all the different resources (e.g., functions and procedures) available in the package; the package body contains the actual Ada code for each of these resources.
In the case of the standard packages (Ada.Text_IO
and
Ada.Calendar
, for example), the Ada source files are often not
supplied with the compiler, since these may be the private property of the
compiler vendor. The executable (precompiled) version of the package body is
supplied, along with a precompiled form of the package specification, often
called a symbol file. These are usually installed along with the compiler and
are usually available to you without further action on your part.
In the case of programmer-defined packages, it is the programmer's
responsibility to write both the specification file and the body file. This
book shows a number of programmer-defined packages, for which the Ada source
code is given.
A Package Containing Minimum and Maximum Functions
Finding the larger or smaller of two numbers is frequently required in programming. The programming task would be made easier, therefore, if we could write functions once to find the minimum and maximum, then package them up for future use. Our first step is to write a package specification. Remember that the specification is a table of contents to the package. This specification can be compiled as it stands, just to be sure there are no compilation errors. The package specification is shown as Program 4.7. Note again the use of pre- and postconditions to document the functions.
Program 4.7
PACKAGE Min_Max IS ------------------------------------------------------------------------ --| Specifications of functions provided by Min_Max package --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the smaller of the two input values FUNCTION Maximum (Value1, Value2: Integer) RETURN Integer; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the larger of the two input values END Min_Max;We now must write the package body. We can incorporate the
Maximum
function written above. Also, we can write the Minimum
function
very easily: Given the Maximum
function, writing a
Minimum
function is just a matter of making a change to the
inequality in the IF
statement:
IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF;
Program 4.8 gives the entire package body. Be certain you understand that the package specification contains the function specifications and the package body contains the function bodies.
Program 4.8
PACKAGE BODY Min_Max IS ------------------------------------------------------------------------ --| Bodies of functions provided by Min_Max package --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer IS Result: Integer; BEGIN IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Minimum; FUNCTION Maximum (Value1, Value2: Integer) RETURN Integer IS Result: Integer; BEGIN IF Value1 > Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Maximum; END Min_Max;
SYNTAX DISPLAY
Package Specification
PACKAGE pname IS list of specifications of resources provided by the package END pname;
PACKAGE MinMax IS FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer; FUNCTION Maximum (Value1, Value2: Integer) RETURN Integer; END MinMax;
SYNTAX DISPLAY
Package Body
PACKAGE BODY pname IS sequence of function and procedure bodies implementing the resources listed in the package specification for pname END pname;
Value1
in the
specification cannot, for example, be called Val1
in the body. Ada
compilers are very fussy about this: Care taken here will avoid
compilation errors.
Find the largest and smallest of three numbers to be provided by the user.
We cannot directly compare the three numbers, so, as in Program 4.3, we will compare them pairwise.
Problem Inputs
the three numbers (Num1, Num2, Num3: Integer
)
Problem Outputs
the largest and smallest numbers (Largest,Smallest: Integer
)
Instead of doing the comparisons directly, we can use the packageMinMax
to find the larger and smaller of pairs of numbers. Given
the three numbers, we can find the smaller of the first two numbers, then find
the smaller of this result and the third number. We can apply the same approach
to finding the largest number.
1. Prompt the user for the three numbers.
2. Find the largest of the three numbers.
3. Find the smallest of the three numbers.
4. Display the results.
Step 2 Refinement:
2.1. Let Largest
temporarily be the larger of Num1
and Num2
.
2.2. Now let Largest
be the larger of itself and
Num3
.
Step 3 Refinement:
3.1. Let Smallest
temporarily be the smaller of Num1
and Num2
.
3.2. Now let Smallest
be the smaller of itself and
Num3
.
Test with different orderings of three integers to be certain that the maximum and minimum are always selected regardless of the original ordering.
The coding is straightforward because our minimum and maximum functions alreadyexist in the package. Assuming that the specification and body for
MinMax
have both been successfully compiled,
Program
4.9 solves the problem. Note the context clause
WITH Min_Max;
at the beginning of the program, along with the other context clauses for the input/output packages.
Program 4.9
WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Min_Max; PROCEDURE Min_Max_Three IS ------------------------------------------------------------------------ --| finds the largest and smallest of three integer values --| using the Minimum and Maximum functions from package Min_Max --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ Num1: Integer; -- program inputs Num2: Integer; Num3: Integer; Largest: Integer; -- program outputs Smallest: Integer; BEGIN -- Min_Max_Three -- prompt user for inputs Ada.Text_IO.Put (Item => "Please enter first integer value > "); Ada.Integer_Text_IO.Get (Item => Num1); Ada.Text_IO.Put (Item => "Please enter second integer value > "); Ada.Integer_Text_IO.Get (Item => Num2); Ada.Text_IO.Put (Item => "Please enter third integer value > "); Ada.Integer_Text_IO.Get (Item => Num3); -- find largest of the three inputs Largest := Min_Max.Maximum(Value1=>Num1, Value2=>Num2); Largest := Min_Max.Maximum(Value1=>Largest, Value2=>Num3); -- find smallest of the three inputs Smallest := Min_Max.Minimum(Value1=>Num1, Value2=>Num2); Smallest := Min_Max.Minimum(Value1=>Smallest, Value2=>Num3); -- display results Ada.Text_IO.Put (Item => "The smallest number is "); Ada.Integer_Text_IO.Put (Item => Smallest, Width => 1); Ada.Text_IO.Put (Item => " and the largest number is "); Ada.Integer_Text_IO.Put (Item => Largest, Width => 1); Ada.Text_IO.New_Line; END Min_Max_Three;Sample Run
Please enter first integer value > -29 Please enter second integer value > 574 Please enter third integer value > 0 The smallest number is -29 and the largest number is 574
Once again, the sample run shows just one test case.
Min_Max
. Copy
the specification and body of Min_Max
, change the name in both
files to Min_Max_Float
, and modify the functions so that
Float
parameters are used instead of Integer
. Write a
program that tests both packages together. (Hint: You will need two
context clauses.)
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.