menu prev next

RECORDS
A record is a user defined data type suitable for grouping data elements together. All elements of an array must contain the same data type.

A record overcomes this by allowing us to combine different data types together. Suppose we want to create a data record which holds a student name and mark. The student name is a packed array of characters, and the mark is an integer.

We could use two seperate arrays for this, but a record is easier. The method to do this is,

The following portion of code shows how to define a record, then create a working variable to be of the same type.

	TYPE   studentname = packed array[1..20] of char;
	       studentinfo = RECORD
				name : studentname;
				mark : integer
			     END;

	VAR    student1 : studentinfo;

The first portion defines the composition of the record identified as studentinfo. It consists of two parts (called fields).

The first part of the record is a packed character array identified as name. The second part of studentinfo consists of an integer, identified as mark.

The declaration of a record begins with the keyword record, and ends with the keyword end;

The next line declares a working variable called student1 to be of the same type (ie composition) as studentinfo.

Each of the individual fields of a record are accessed by using the format,

      recordname.fieldname := value or variable;

An example follows,

      student1.name := 'JOE BLOGGS          ';  {20 characters}
      student1.mark := 57;


Copyright B Brown/P Henry/CIT, 1988-1996. All rights reserved.
menu prev next

1