What are records in Pascal?

Overview

The records data type in Pascal lets you store the different properties (of varying data types) for one type of object. records is similar to structs in C and C++. Suppose you own a showroom and wish to keep track of a car’s model, year of making, and color. Using the records data type in Pascal, you can store each car’s data in a separate variable of type records. Below is an example of how records are defined in a program:

type
name-of-record = record
   property-1: data-type1;
   property-2: data-type2;
   ...
   property-n: data-typen;
end;

The variables of type name-of-record are defined as shown below:

var
var1:name-of-record;

Basic example

Below is a basic example of how to use records through an implementation of the ‘cars showroom’ example given above. The code first defines records named Cars with fields referring to the car’s model,yearofmaking, and color. Then, it defines a variable of type Cars records and assigns values to each of the fields.

Note: You can ignore the stdout from Free Pascal Compiler... till ...compiled, 0.0 sec. It is a bug in some versions of LD.

program recordsExample;
type
Cars = record
model : string;
yearofmaking : string;
color : string;
end;
var
car1: Cars;
begin
car1.model := 'Beetle';
car1.yearofmaking := '2000';
car1.color := 'White';
writeln('Car1 Model: ', car1.model);
writeln('Car1 Year: ', car1.yearofmaking);
writeln('Car1 Color: ', car1.color);
end.

Ways of accessing fields

There are multiple ways that the fields of a record can be accessed. First is the (.) operator method, which is used in the previous example to assign and print values of a field. Second is the With method that can be used as shown below:

program recordsExample;
type
Cars = record
model : string;
yearofmaking : string;
color : string;
end;
var
car1: Cars;
begin
With car1 do
begin
model := 'Beetle';
yearofmaking := '2000';
color := 'White';
end;
writeln('Car1 Model: ', car1.model);
writeln('Car1 Year: ', car1.yearofmaking);
writeln('Car1 Color: ', car1.color);
end.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved