Real life and all academic disciplines present situations where data of various types or multiple data items of the same type can be attributed to a single object. For example, a person can possess both a name and an age, which would obviously be stored in different kinds of variables -- a string for the name and probably an integer for the age. In mathematics, a point on a graph would be represented by numbers for the coordinates, both of which could be of types integer or real.
These aggregates of information can be stored in unrelated variables with names that have some portion of the names in common. For example, we could use xcoord and ycoord for the coordinates of a point. However, this has the disadvantage of not associating the variables as closely as we would like.
Pascal provides a construct that makes the relationship clear and allows you to give a cover name to the object that you create. This construct is known as a record and is introduced with the keyword record.
Three sample programs are provided at the end of this page:
The definition below groups three characteristics of a triangle into a single record called triangle_rec.
triangle_rec = RECORD
b : real; { base }
h : real; { height }
A : real; { Area }
END;
What this definition does not do is get any memory allocated -- what it does do is create a new programmer-defined data type. Consequently, no data can be stored in triangle_rec. To get memory allocated, we need to declare a data structure of this type, such as is done below.
tri : triangle_rec;
This record consists of three member fields which can be accessed by means of a field selector, which is separated from the record name by a dot/period. The following example assigns the value 13.2 to the base field.
tri.b := 13.2
This is an interactive program using a procedure to calculate the area of a triangle. All data concerning the triangle is contained in a record of type triangle_rec.
program TriangleAreaInteractive (input, output);
type
triangle_rec = RECORD
b : real; { base }
h : real; { height }
A : real; { Area }
END;
var
tri : triangle_rec;
procedure calctri(var t : triangle_rec);
{Calculate the area of a triangle, given its base & height }
begin
t.A := t.b * t.h / 2
end;
begin
{-------------------Introduction------------------}
writeln('This program calculates and prints the area of a triangle');
writeln('after you enter its dimensions.');
writeln('When asked to, type in a dimension and hit the ENTER key.');
writeln;
{-------------------Inputting Data------------------}
write('What is the triangle''s base? ');
readln(tri.b);
write('What is the triangle''s height? ');
readln(tri.h);
{-------------------Calculation------------------}
calctri(tri);
{-------------------Printing Results------------------}
writeln;
writeln('The area of a triangle with a base of ', tri.b:0:2, ' units and');
writeln('a height of ', tri.h:0:2, ' units is ', tri.A:0:2, ' square units.')
end.
This program assigns values to two records in an array and then writes them to a file of type point_rec.
program Write_Rec;
{Purpose: Write an array of point records to a file of type point_rec}
type
point_rec = RECORD
X : integer; { X coordinate }
Y : integer { Y coordinate }
END;
var
point : array [1..2] of point_rec;
I : integer; {Loop Control Variable}
PointFile : file of point_rec;
begin
{-------------------Assign Values------------------}
point[1].X := 1;
point[1].Y := 2;
point[2].X := 3;
point[2].Y := 6;
{------------------Open File---------------}
Assign(PointFile, 'a:\points.rec');
Rewrite(PointFile);
{-------------Write Data to File-----------}
for I := 1 to 2 do
write(PointFile, point[I]);
{------------------Close File---------------}
Close(PointFile)
end.
This program fills an array of records with coordinates of two points from a text file and calls a function which calculates and return the slope of the line which runs through them.
program Read_Rec (output);
{Purpose: Reads coordinates of 2 points from a text file
and calculates the slope of the line through them}
type
point_rec = RECORD
X : integer; { X coordinate }
Y : integer { Y coordinate }
END;
function calcslope(P1, P2 : point_rec) : real;
{Returns the slope of the line through points P1 and P2}
{N.B. No bulletproofing for vertical line!!!}
begin
calcslope := (P1.Y - P2.Y) / (P1.X - P2.X)
end;
var
point : array [1..2] of point_rec;
I : integer; {Loop Control Variable}
Slope : real;
PointFile : Text;
begin
{------------------Open File---------------}
Assign(PointFile, 'a:\pointrec.txt');
Reset(PointFile);
{-------------Read Data from File-----------}
for I := 1 to 2 do
begin
readln(PointFile, point[I].X);
readln(PointFile, point[I].Y)
end;
{------------------Close File---------------}
Close(PointFile);
{-----------------Calc Slope----------------}
Slope := calcslope(Point[1], Point[2]);
{-----------------Report Result-------------}
writeln('The slope of the line through the points whose ');
write('coordinates are (', Point[1].X, ',', Point[1].Y, ') and ');
writeln('(', Point[2].X, ',', Point[2].Y, ') is ', Slope:0:2, '.');
end.