ZetCode

Ada records

last modified November 23, 2026

A record groups components of possibly different types under one name. Where an array reaches many values of one type by position, a record reaches a few named parts, such as the coordinates of a point or the fields of a date. A record type declares the component names and types; an object of that type is created from an aggregate or component by component. This article builds records from a simple point to nested records, default values, arrays of records, and a small complex-number type.

Record types and aggregates

A record type is written record ... end record, with one line per component. The component names are visible from outside the type through dot notation. An aggregate for a record uses parentheses and may name each component, which is clearer than relying on declaration order. Unlike an array aggregate, a record aggregate still uses parentheses in Ada 2022.

point_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Point_Demo is
   type Point is record
      X : Integer;
      Y : Integer;
   end record;

   P : Point := (X => 3, Y => 4);
begin
   Put_Line ("x =" & Integer'Image (P.X));
   Put_Line ("y =" & Integer'Image (P.Y));
   P.X := P.X + 10;
   Put_Line ("new x =" & Integer'Image (P.X));
end Point_Demo;

The aggregate (X => 3, Y => 4) gives both components, and the selections P.X and P.Y read them. A component behaves like any other variable of its type, so P.X := P.X + 10 changes it while leaving P.Y alone. The same aggregate could be written positionally as (3, 4), because the components are taken in declaration order.

$ gnatmake -gnat2022 -gnatwa point_demo.adb
$ ./point_demo
x = 3
y = 4
new x = 13

Nested records

A component may itself be a record. The inner record has its own type and aggregate, and selection follows the chain of names from the outer object to the innermost component. This models a value that naturally contains another value, such as an event with a date, without flattening all the fields into one long record.

event_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Event_Demo is
   type Date is record
      Year  : Integer;
      Month : Integer;
      Day   : Integer;
   end record;

   type Event is record
      Title   : String (1 .. 12);
      Date_Of : Date;
   end record;

   E : Event := (Title => "Ada birthday",
                 Date_Of => (Year => 1983, Month => 2, Day => 17));
begin
   Put_Line (E.Title);
   Put_Line ("year =" & Integer'Image (E.Date_Of.Year));
   Put_Line ("day =" & Integer'Image (E.Date_Of.Day));
   E.Date_Of.Day := 18;
   Put_Line ("new day =" & Integer'Image (E.Date_Of.Day));
end Event_Demo;

The outer aggregate contains a nested aggregate for the date, and E.Date_Of.Year reaches through two levels. The title is a fixed string of twelve characters, a subtype that the compiler checks when the aggregate is built. Because the path ends in a variable component, assigning to E.Date_Of.Day is allowed and updates only that field.

$ gnatmake -gnat2022 -gnatwa event_demo.adb
$ ./event_demo
Ada birthday
year = 1983
day = 17
new day = 18

Default component values

A component declaration may end with := expression, which supplies a value used when an object is created without an aggregate. Defaults keep an object usable immediately: a name starts blank and an age starts at zero. Every component still has a type, so a default must belong to that type.

defaults.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Defaults is
   type Person is record
      Name : String (1 .. 10) := [others => ' '];
      Age  : Natural := 0;
   end record;

   P : Person;
begin
   P.Name (1 .. 3) := "Ada";
   P.Age := 36;
   Put_Line (P.Name (1 .. 3) & " is" & Natural'Image (P.Age));
end Defaults;

P is declared without an aggregate, so the defaults fill both components: ten spaces and zero. The assignment writes the three characters of Ada into the first three positions of the name, and the rest stay blank. The final line prints only the filled slice, because printing the whole component would show the trailing spaces. A string component has a fixed length, so a longer name would need a longer component or an unbounded string.

$ gnatmake -gnat2022 -gnatwa defaults.adb
$ ./defaults
Ada is 36

Arrays of records

Records and arrays combine: an array element type may be a record, and each element is then reached by an index followed by a component name. The aggregate of such an array is a bracketed list of record aggregates, so the outer brackets belong to the array and the inner parentheses to each record.

route.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Route is
   type Point is record
      X : Integer;
      Y : Integer;
   end record;

   type Path is array (1 .. 4) of Point;
   Route : constant Path := [(0, 0), (1, 2), (3, 3), (5, 1)];

   Sum_X : Integer := 0;
   Sum_Y : Integer := 0;
begin
   for I in Route'Range loop
      Sum_X := Sum_X + Route (I).X;
      Sum_Y := Sum_Y + Route (I).Y;
   end loop;
   Put_Line ("x total =" & Integer'Image (Sum_X));
   Put_Line ("y total =" & Integer'Image (Sum_Y));
   Put_Line ("last x =" & Integer'Image (Route (Route'Last).X));
end Route;

Each element of Route is a point written as a positional record aggregate. The loop adds the X and Y components separately, giving 9 and 6, and the last line selects X from the final element. The array is indexed from 1, so Route'Last is 4.

$ gnatmake -gnat2022 -gnatwa route.adb
$ ./route
x total = 9
y total = 6
last x = 5

Equality and assignment

The predefined equality operator compares two records component by component: they are equal when every corresponding component is equal. Assignment copies all components from the source to the target, so the two objects remain independent afterwards. As with other types, the comparison and the assignment require matching record types.

equality.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Equality is
   type Point is record
      X : Integer;
      Y : Integer;
   end record;

   function Same (Left, Right : Point) return Boolean is
     (Left = Right);

   A : constant Point := (1, 2);
   B : Point := (1, 2);
   C : constant Point := (2, 1);
begin
   Put_Line ("a = b =" & Boolean'Image (Same (A, B)));
   Put_Line ("a = c =" & Boolean'Image (Same (A, C)));
   B := C;
   Put_Line ("b = c =" & Boolean'Image (Same (B, C)));
end Equality;

A and B have the same two components, so Same returns true; A and C differ, so it returns false. The assignment B := C copies both components, after which B and C compare equal. The helper function keeps the comparison in one place; it does not change the meaning of =.

$ gnatmake -gnat2022 -gnatwa equality.adb
$ ./equality
a = b =TRUE
a = c =FALSE
b = c =TRUE

An applied example: complex numbers

A record is a natural representation for a mathematical value with several parts. A complex number has a real and an imaginary component, and multiplication combines them into a new pair. The function below returns a record aggregate, so the whole result travels as one value.

complex_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Complex_Demo is
   type Complex is record
      Re : Float;
      Im : Float;
   end record;

   function Multiply (A, B : Complex) return Complex is
     ((Re => A.Re * B.Re - A.Im * B.Im,
       Im => A.Re * B.Im + A.Im * B.Re));

   P : constant Complex := (1.0, 2.0);
   Q : constant Complex := (3.0, 4.0);
   R : constant Complex := Multiply (P, Q);
begin
   Put_Line ("real =" & Float'Image (R.Re));
   Put_Line ("imag =" & Float'Image (R.Im));
end Complex_Demo;

The formula multiplies (1 + 2i) by (3 + 4i) and produces -5 + 10i: the real part is 1*3 - 2*4 and the imaginary part is 1*4 + 2*3. The function is an expression function whose body is a record aggregate, which is why the parentheses are doubled. The components of R are printed separately, since Float'Image formats a single number rather than a complex value with an imaginary part.

$ gnatmake -gnat2022 -gnatwa complex_demo.adb
$ ./complex_demo
real =-5.00000E+00
imag = 1.00000E+01

Next steps

The next article, discriminated and variant records, adds a component that controls which other components exist. Such a discriminant lets one record type represent a circle or a rectangle, a number or a word, while the compiler checks that only the components of the active variant are read.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared record types, built aggregates with named and positional components, nested one record inside another, given components default values, stored records in an array, compared and copied records, and returned a record from a function. The arrays article covers the bracketed aggregates used for the array and for string components.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.