Record type


The record type allows you to create your own type that is a collection of other types. For example, if you are writing a game where the user controls a game hero on the screen, you might use the following record type to describe the hero's position and health:

  type heroType = record
     positionX, positionY: integer;
     health: integer;
  end;

To create the variable that has the newly defined type, use the normal 'var' statement:

var hero: heroType;

You can also use the new type 'heroType' when defining procedures or functions:

  function isHeroDead(hero: heroType): boolean;
  begin
  if (hero.health <= 0) then
      isHeroDead := true;
    else
      isHeroDead := false;
  end;

Record type can be defined directly inside the 'var' statement:

  var hero: record
              positionX, positionY: integer;
              health: integer;
            end;

To access an element of the record, use the dot operator:

  ...
  { move the hero to the right }
  hero.positionX := hero.positionX + 1;
  ...

You cannot directly copy one record to another:

  var a, b: record
              x: integer;
	      end;
  begin
    ...
    a := b; { this is not valid }
    ...
    a.x := b.x; { copy element by element instead }
  end.