Section 2.3 - Packages

The package is Ada's basic unit for defining a collection of logically related entities. For example, a package can be used to define a set of type declarations and their associated operations.

For example, all Ada compilers provide a package called Text_IO. Here's a highly simplified version of the package declaration of the package Text_IO (the lengthy complete definition is part of the RM's appendix A):

package Text_IO is
  type File_Type is limited private;
  type File_Mode is (In_File, Out_File, Append_File);
  procedure Create (File : in out File_Type;
                    Mode : in File_Mode := Out_File;
                    Name : in String := "");
  procedure Close (File : in out File_Type);
  procedure Put_Line (File : in File_Type; Item : in String);
  procedure Put_Line (Item : in String);
end Text_IO;  

The package declaration of package Text_IO defines a type called `File_Type' that represents an opened or created file. The phrase `limited private' means that there are no predefined operations on this type (more about that later).

Package Text_IO also defines a type called `File_Mode'; values of this type can only have one of three values (this is how enumeration types are defined in Ada).

The type definitions are followed by a set of subprograms that can accept values of type File_Type. Note that procedures (subprograms) can be contained inside packages. Procedure `Create' lets you create files with given names; procedure `Close' closes a file.

There are two procedures named `Put_Line' which write text out, but they differ in the arguments they can accept. The first takes a file and the string to be output, while the other just takes the string to be output.

If a package declaration includes other declarations inside it then there must be a package body somewhere that includes the bodies of the items declared. This simplified package declaration for Text_IO has procedure declarations, so somewhere else there must be a package body for Text_IO.


Quiz:

In this simplified package declaration for package Text_IO, how many subprograms have been defined that explicitly require a File_Type parameter?

  1. One
  2. Two
  3. Three
  4. Four

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 2 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s2s3.htm".