Previous topic | Next topic | Ada Home Page | Index

Declaring records

Declaring record types

Each component of a record is called a field.

Declaring a record involves declaring the name and type of each field, in a record structure which itself is given a name.

Form of declaration:

   -- declaration of record data type
   type record_type_name is record
       field_name_1 : field_type_1;
       field_name_2 : field_type_2;
       -- various fields in the record
   end record;

Note that the field types need to be declared before they can be used in a record declaration.

Declarations of field types often involve constants, for things like bounds of subrange types, or sizes of strings. Thus a common pattern for the declarations in a program is:

  1. constants
  2. elementary data types
  3. structured data types

Declarations for the fitness club example fit this pattern.

Style suggestions:


Declaring record variables

Once record types are declared, you can then declare variables of the record type:

    this_person,
    that_person : persons;


Initialising records

A default value can be specified for fields

    field_name: f_type := (others => space);

You can use an aggregate to set values to the fields of a record.

Example - positional aggregate:

   average_male : constant persons :=
       ("Mr. A Average            ",
        "          ",
        male, 34, 65.5);

Example - named aggregate:

   average_female : constant persons :=
       (name=>"Ms. A Average            ",
        phone=>"          ",
        sex=>female, age=>32, 
        weight=>52.0);


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96