In contrast to the entities declared in the visible part of a package specification, the entities declared in the package body are only visible within the package body itself. As a consequence, a package with a package body can be used for the construction of a group of related subprograms (a package in the usual sense), in which the logical operations available to the users are clearly isolated from the internal entities.
For the elaboration of a package body, its declarative part is first elaborated, and its sequence of statements (if any) is then executed. The optional exception handlers at the end of a package body service exceptions raised during the execution of the sequence of statements of the package body.
Notes:
A variable declared in the body of a package is only visible within this body and, consequently, its value can only be changed within the package body. In the absence of local tasks, the value of such a variable remains unchanged between calls issued from outside the package to subprograms declared in the visible part. The properties of such a variable are similar to those of an "own" variable of Algol 60.
The elaboration of the body of a subprogram declared in the visible part of a package is caused by the elaboration of the body of the package. Hence a call of such a subprogram by an outside program unit raises the exception PROGRAM_ERROR if the call takes place before the elaboration of the package body (see 3.9).
Example of a package:
package RATIONAL_NUMBERS is
type RATIONAL is record NUMERATOR : INTEGER; DENOMINATOR : POSITIVE; end record;
function EQUAL(X,Y : RATIONAL) return BOOLEAN;
function "/" (X,Y : INTEGER) return RATIONAL; -- to construct a rational number function "+" (X,Y : RATIONAL) return RATIONAL; function "-" (X,Y : RATIONAL) return RATIONAL; function "*" (X,Y : RATIONAL) return RATIONAL; function "/" (X,Y : RATIONAL) return RATIONAL; end;
package body RATIONAL_NUMBERS is
procedure SAME_DENOMINATOR (X,Y : in out RATIONAL) is begin -- reduces X and Y to the same denominator: ... end;
function EQUAL(X,Y : RATIONAL) return BOOLEAN is U,V : RATIONAL; begin U := X; V := Y; SAME_DENOMINATOR (U,V); return U.NUMERATOR = V.NUMERATOR; end EQUAL;
function "/" (X,Y : INTEGER) return RATIONAL is begin if Y > 0 then return (NUMERATOR => X, DENOMINATOR => Y); else return (NUMERATOR => -X, DENOMINATOR => -Y); end if; end "/";
function "+" (X,Y : RATIONAL) return RATIONAL is ... end "+"; function "-" (X,Y : RATIONAL) return RATIONAL is ... end "-"; function "*" (X,Y : RATIONAL) return RATIONAL is ... end "*"; function "/" (X,Y : RATIONAL) return RATIONAL is ... end "/";
end RATIONAL_NUMBERS;
References: declaration, declarative part, elaboration, and 3.9, exception, exception handler, name, package specification, program unit, program_error exception, sequence of statements, subprogram, variable, visible part.