1. What are the benefits of records in Pascal?
Records allow you to group different types of data on certain grounds. The fields of records may be variables of different types.
– a record that contains information about the coordinates of point on the plane (x; y);
– a record that contains information about name, surname and patronymic of worker;
– a record that contains information about the book in library: name of reader, address of reader.
Using of records is the convenient way, when you need to save and process the arrays of variables of different types.
2. What types of variables you may use in the records?
In the records variables can be different types. It can be and standard types of data and user data types. This significantly differs the record from array.
3. What is the general view of record?
In the application you can define the record by two ways:
– by using a definition of record’s type in the section type;
– by using a definition of variable of record’s type in the section var.
Way 1. Record’s definition by using of section of type definition “type”. The result is a its own type, which is a record. With the help of this way it is possible to use the variables that are records in the procedures and functions as parameters.
type <Name_type_record> = record <field1:type> <field2:type> ... <fieldN:type> end;
Name_type_record – the name of type, which is assigned to the variables of this record;
field1, field2, fieldN – the names of variables which are the fields of record;
type – the name of of some type from allowable types of program (integer, real, string …);
After this describing it is possible to define the variable of these type in the section var:
Way 2. Definition of record immediately in the section var.
var <Variable_name>: record <field1:type> <field2:type> ... <fieldN:type> end;
4. An example of definition and using the record, which describes the coordinates of point on the plane (x; y).
Way 1. By using the type section of type’s definition.
type // The 'TPoint' record definition TPoint = record x:real; // x coordinate y:real; // y coordinate end; var P:TPoint; // variable named 'P' of TPoint type
To save the values of coordinates in the variable P, you need to write the following text:
... begin ... P.x := 12.5; P.y := -8.3; ... end; ...
Way 2. In the var section of variables definition.
var P2:record // variable record has two fields x and y x:real; y:real; end;
To access to the fields of variable P2 you need to write following:
... begin ... P2.x := -9.25; P2.y := 18; ... end; ...
5. An example of definition and using of record of type TBOOK.
An example of definition the record of TBOOK type that includes following fields:
– the book’s title;
– author’s name;
– year of publication of the book;
– the cost of the book.
type TBOOK = record Title:string; Author:string; Year:integer; Price:real; end; var B:TBOOK; ... begin ... B.Title := 'Java 8'; B.Author := 'Herbert Schildt'; B.Year := 2015; B.Price := 0.00; ... end;
6. How to assign one record to another record?
To assign the value of fields from one record to another record these records must be the same type. This type must be defined in the section type.
In the example below, the assignment between variables M1 and M2 is realized. Two variables are the records of type TMonth.
type // Definition of record that describes the month of the year TMonth = record num:1..12; // the month in the year (1-january, 2-february, ...) name:string; // the name of month quarter:1..4; // number of quarter end; var M3:record num:1..12; // the month in the year (1-january, 2-february, ...) name:string; // the name of month quarter:1..4; // number of quarter end; M1,M2:TMonth; begin ... M1.num := 2; M1.name := 'Февраль'; M1.quarter := 1; // assignment between records M2:=M1; // Error - Incompatible types: 'Record' and 'TMonth' // M3:=M1; ... end;
7. An example of definition and using of one-dimensional array of records of type BOOK.
... type TBOOK = record Title:string; Author:string; Year:integer; Price:real; end; var Books:array[1..20] of TBOOK; begin ... Books.Title := 'C# 4.0'; Books.Author := 'Herbert Schildt'; Books.Year := 2011; Books.Price := 0.99; ... end;
8. An example of definition and using the nested records.
In the example below two records of types TName and TBOOK are defined. In the record of TBOOK type, the variable of type TName is defined.
type TName = record Name:string; Surname:string; end; TBOOK = record Title:string; Author:TName; // nested record Year:integer; Price:real; end; var Books:array[1..20] of TBOOK; B:TBOOK; begin B.Title := 'C# 4.0'; B.Author.Name := 'Herbert'; B.Author.Surname := 'Schildt'; B.Year := 2011; B.Price := 0.99; end;
9. What are the advantages of With operator in the records?
The “with” statement allows you to reduce the long notation of elements records. “with” statement also improves readability and reduces typing the text of the program in which there is frequent reference to the record fields. In addition, the with statement reduces the execution time of the program, because link to the record is carried out only once.
The “with” statement opens a scope that includes field names a variable of type “Record”.
The general form of “with” statement:
with <Variable_name> do statement;
The statement with can be composit:
with <Variable_name> do begin statement1; statement2; ... statementN; end;
10. An example of using “with” statement.
type TName = record Name:string; Surname:string; end; TBOOK = record Title:string; Author:TName; // nested record Year:integer; Price:real; end; var B:TBOOK; begin with B do begin Title := 'C# 4.0'; with Author do begin Author.Name := 'Herbert'; Author.Surname := 'Schildt'; end; Year := 2011; Price := 0.99; end; end;
11. What benefits of using the records with variants?
In records it is possible to set the type, that includes definitions of several options of structure.
The record with variants – this is the variable that can include objects of different types and sizes. Compiler performs all requirements for sizes and alignment. Records with variants allows you to save data in the memory.
A record may include only one part of variant (in the section “case“). If you use the variants parts in the program, it minimizes the memory allocation. It is effective in the program when you use the arrays of records that contain many fields.
12. An example of using the variant’s record.
In the example below is the definition and using the variant’s record. This record describes whether the worker of the institution is in a particular category of workers.
According to category, the corresponding fields of worker are formed. The category of worker is saved in the variable f_worker.
type TName = record Name:string; Surname:string; end; // The record with variant, that describes of worker of institution TWorker = record name:TName; // Name of worker birth:string; // birth date // The worker can be in the one of three categories case f_worker:1..3 of 1: // if the administration (post_adm:string; // position rate:real; // rate: 0.5, 0.75, 1.0 salary:real); // the salary 2: // if the teacher (hours:integer; // teaching load, hours fee: real); // payment per hour 3: // if support personnel (post_pers:string; // name of position rank:integer; // wage category tariff:real); // tariff rate end; var W1:TWorker; begin W1.name.Name := 'Ivan'; W1.name.Surname := 'Kornilenko'; W1.birth := '19.02.1988'; // If worker is administration then W1.f_worker := 1; // administration W1.post_adm := 'Director'; W1.rate := 0.75; W1.salary := 18800.50; // If worker is a teacher then W1.f_worker := 2; W1.hours := 72; W1.fee := 230.55; // If worker is personnel then W1.f_worker := 3; W1.post_pers := 'Driver'; W1.rank := 3; W1.tariff := 14390.35; end;
The record with variants includes the “case…of” part. Compiler interprets the fields of record according to the value of variable f_worker.