Skip to content
Records
step 1/5

Reading — step 1 of 5

Learn

~1 min readRecords and Arrays

A record in Pascal is a struct — fields grouped under a single name.

type
    TPerson = record
        Name: string;
        Age: integer;
    end;

var
    p: TPerson;
begin
    p.Name := 'Ada';
    p.Age := 36;
    WriteLn(p.Name, ' is ', p.Age);
end.

The TPerson naming convention (T-prefix for type) is from Delphi/FPC tradition.

The with keyword — convenience for setting many fields:

with p do begin
    Name := 'Ada';
    Age := 36;
end;

Records can nest:

type
    TPoint = record X, Y: integer; end;
    TLine = record
        Start, Finish: TPoint;
    end;

Variant records — like C unions, multiple shapes sharing memory (use sparingly):

type
    TShape = record
        case Kind: (sCircle, sSquare) of
            sCircle: (Radius: real);
            sSquare: (Side: real);
    end;

Records by value — Pascal records are passed by value by default. Use var parameter for pass-by-reference:

procedure Reset(var p: TPerson);
begin
    p.Name := '';
    p.Age := 0;
end;

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…