Skip to content

By coding a Record

Memory-representation
Memory representation with record

We commonly think to a record like something to be registered or annotated; musical, medical and data recording, and so on.
But structurally... what is a record?

Definition is so simple to result boring: a record is a structure (collection) of different nature data.

In the previous article we saw an important structure called array: a collection of homogeneous data.
An array can host only one kind of data per time: integer numbers or characters or arrays again...

Records offer one more degree of liberty: they can contain integers and chars and arrays... and records as well!

So a sport record is a possible collection of: first and last name (chars or strings), kind of sport (chars or strings) and the performance (typically a number).

Performance is what we commonly intend with sport record, but this is an example of metonymy (yes, we are poets too :)).

But how to treat records in FreePascal? See below:

type
  TSuit = (Hearts, Diamonds, Clubs, Spades);

type
  TRank = (Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
           Jack, Queen, King);

type TCard = record
  Suit : TSuit;
  Rank : TRank;
end;

type TPlayer = record
  Name : string;
  Cards : array of TCard; //Dynamic array of Cards
end;

We define a record's type, with name TCard.
Pay attention: we're not defining a record variable, but just a name to let us remember what kind of record it is.
By choosing the correct name you save yourself much time; you avoid to force yourself reading again its composition.
It's so important just like inserting comments.

Well... what's there?
A record of two previously defined types called TSuit and TRank, which are both enumeration types, ordinal ones.
Look at the first TSuit: essentially the four internal arguments are a mask for integer ordinal number, from 1 to a number equal to arguments one... we have here from 1 to 4.
TRank: it goes from 1 to 13.

In other words the enumeration type is useful when you want to describe something that clearly has an order, or which you want to give to.
The link between names and numbers is under your choice; nobody can prevent you from selecting a different order: just remember that the beneath order is fixed... more or less.

Type
  EnumType = (one, two, three, forty := 40,fortyone);

Which sequence here?
1, 2, 3, 40 and...? 4 or 41?
Enumeration just adds to next item 1 integer value more than the previous, so fortyone is exactly 41.