Last time we looked at user-defined types in Pascal. This time we'll cover one of the structured data types, namely RECORD.
What RECORD Is
RECORD is a structured data type in Pascal, built from a collection of standard and user-defined data types.
The job of a RECORD is to tie several different data types into one.
Picture this.
A school wants to store data about its students. That student data is made up of several parts:
- Full name
- Nickname
- Class
- Age
- Gender
To stay structured, that data has to be written on one sheet of paper. You can't put the full name and the class on different sheets. That way is inefficient.
The role RECORD plays here is that single sheet of paper, holding several pieces of student data together.
Writing a RECORD in Pascal
A RECORD has to be declared first in the type part, because it will be used as a data type later.
type
record-name = record
field-1: data-type;
field-2: data-type;
...
field-n: data-type;
end;
Here is how to declare a RECORD for student data.
type
Data_Siswa = record
nama_lengkap : string;
nama_panggilan: string;
kelas : integer;
umur : integer;
jenis_kelamin : string;
end;
var
siswa: Data_Siswa; // declare a variable with the RECORD type "Data_Siswa"
Or, to save time, the RECORD declaration can be used directly as the type of a variable.
var
siswa: record
nama_lengkap : string;
nama_panggilan: string;
kelas : integer;
umur : integer;
jenis_kelamin : string;
end;
I recommend adding extra spaces in a RECORD declaration so the colons line up, because it makes the code easier to read.
Accessing Values in a RECORD
To access a value in a RECORD, whether you're filling it in or reading it out, use a dot after the record variable name.
To see it clearly, let's go straight to a simple program.
program BelajarRecord;
uses crt;
type
Data_Siswa = record
nama_lengkap : string;
nama_panggilan: string;
kelas : integer;
umur : integer;
jenis_kelamin : string;
end;
var
siswa: Data_Siswa;
begin
clrscr;
siswa.nama_lengkap := 'Budi Pekerti';
siswa.nama_panggilan := 'Budi';
siswa.kelas := 10;
siswa.umur := 15;
siswa.jenis_kelamin := 'Laki-laki';
// print the contents of the Data_Siswa RECORD
writeln('nama lengkap: ', siswa.nama_lengkap);
writeln('nama panggilan: ', siswa.nama_panggilan);
writeln('kelas: ', siswa.kelas);
writeln('umur: ', siswa.umur);
writeln('jenis kelamin: ', siswa.jenis_kelamin);
readln;
end.
In that example we fill the RECORD variable siswa with a value for each field, then print it. The output will look something like this.
nama lengkap: Budi Pekerti
nama panggilan: Budi
kelas: 10
umur: 15
jenis kelamin: Laki laki
The values inside a RECORD variable behave like ordinary variables, so you can run arithmetic or boolean operations on them.
Looking at that code, it does seem long. Is there a way to make it shorter?
There sure is...
To shorten how we call the RECORD data type, we can use the WITH DO statement.
Using WITH DO to Access RECORD Values
WITH DO lets us work with a RECORD all at once. When you access values inside it, you no longer have to type the variable name.
Let's modify the code we wrote earlier to use WITH DO.
program BelajarRecord;
uses crt;
type
Data_Siswa = record
nama_lengkap : string;
nama_panggilan: string;
kelas : integer;
umur : integer;
jenis_kelamin : string;
end;
var
siswa: Data_Siswa;
begin
clrscr;
with siswa do // using the WITH DO statement
begin
nama_lengkap := 'Budi Pekerti';
nama_panggilan := 'Budi';
kelas := 10;
umur := 15;
jenis_kelamin := 'Laki-laki';
end;
// print the contents of the Data_Siswa RECORD
with siswa do
begin
writeln('nama lengkap: ', nama_lengkap);
writeln('nama panggilan: ', nama_panggilan);
writeln('kelas: ', kelas);
writeln('umur: ', umur);
writeln('jenis kelamin: ', jenis_kelamin);
end;
readln;
end.
If you run it, the output will be the same as before.
What WITH DO does here is let you work with the fields on their own. Every variable between BEGIN and END in that block then refers to the RECORD variable automatically.
So it's cleaner, isn't it :D
Wrapping Up
That's enough for this tutorial, hope it helps :D
Next time we'll cover arrays in Pascal.