Section 6.6 - Records

Types can be a complex collection of other types; the primary method for collecting these is through the record (which is basically identical to the Pascal record and C struct). For example, here's an example of a record useful for recording dates:

 type Date is
  record
   Day   : Integer range 1 .. 31;
   Month : Integer range 1 .. 12;
   Year  : Integer range 1 .. 4000 := 1995;
  end record;

The record component `Year' has an example of an `initialization clause' - any object created with this type automatically has initial values given in initialization clauses.

Creating variables of a record type is done the same way as any other type. A record component is referenced by using the variable name, a period, and the name of the record component. For example, let's create a variable called Ada_Birthday, and set its values to December 10, 1815:

 procedure Demo_Date is
   Ada_Birthday : Date;
 begin
   Ada_Birthday.Month := 12;
   Ada_Birthday.Day   := 10;
   Ada_Birthday.Year  := 1815;
 end Demo_Date;

Quiz:

If you had the following record type:

 type Complex is
  record
    Real_Part, Imaginary_Part : Float := 0.0;
  end record;

and you declared the following type:

  X : Complex;

How would you set X's Real_Part to 1?

  1. Complex.X.Real_Part := 1.0;
  2. X.Real_Part := 1.0;
  3. Real_Part.X := 1.0;

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 6 outline

David A. Wheeler (dwheeler@dwheeler.com)

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