Pascal Records

Records
A record is a collection of variables (fields about a given entity). These variables can be of different data types. Elements in a record are called members.

Record declaration
A record can be declared in two ways, either through type declaration or through variable declaration.

Declaring a record through type declaration
Type studrec = record stdName: string; stdExamNo: integer; score: integer; end;

Defining of a record variable
Before assigning values to the members of the record, the record should first be defined as a record variable. Example stdmarksRec : studrec; This defines stdmarkrec as a variable of type stud rec. The definition causes the program to reserve sufficient space in memory for stdarkrec.

Declaring a record through variable declaration
It is used when there is no need to pass a record to a procedure or a function var stdrmarkRec : record stdName: string; stdExamNo: integer; score: integer; end;

Accessing a member of a record
When a record variable is defined its members can then be accessed using two methods:
 * 1) The dot / member operator.
 * 2) With structure.

Using the dot operator
The member in a record is accessed through its record name followed by the member name after the period.

Syntax: recordname.membername; Example 1: stdmarkRec.stdName := 'Theocoles'; stdmarkRec.stdExamNo := '5678'; Example 2: Record illustration. program recordsuse(input, output); Type studrec = record stdName: string; stdExamNo: integer; cat1, cat2, exam: integer; end; var stdmarkRec : studrec; total : integer; begin writeln('Enter student name'); readln(stdmarkRec.stdName); writeln('Enter marks for cat1, cat2 and exam'); readln(stdmarkRec.cat1, stdmarkRec.cat2, stdmarkRec.exam); total := ((stdmarkRec.cat1 + stdmarkRec.cat2) * 30/200) + (stdmarkRec.exam *70/100); writeln('Student Name : ', stdmarkRec.stdName); writeln('Total marks : ', total); end.

Using the with structure
Used mostly in situations where there are many fields (members) in a record, especially effective in reducing the size of cord.

Syntax: with recordvariable do  begin statements; end; Example: program recordsuse(input, output); Type studrec = record stdName: string; stdExamNo: integer; cat1, cat2, exam: integer; end; var stdmarkRec : studrec; total : integer; begin with stdmarkRec do    begin writeln('Enter student name'); readln(stdName); writeln('Enter marks for cat1, cat2 and exam'); readln(cat1, cat2, exam); total := ((cat1 + cat2) * 30/200) + (exam *70/100); end; writeln('Student Name : ', stdmarkRec.stdName); writeln('Total marks : ', total); end.

Array of Records
Similar to an array of variables (e.g. array of integers), it is useful in manipulation of records (such as comparing records, or arranging of records in a certain sequence).

Syntax: var stdmarkRec : Array [1..5] of studRec; The above syntax is an example to create five(5) sets of records that are organized as defined in studRec. Where studRec is already declared in the Type declaration section. see record declaration.

Example: program recordarrays(input, output); Type studrec = record stdName: string; stdExamNo: integer; math, eng, chem: integer; end; var stdmarkRec : Array[1..5] of studrec; total : Array[1..5] of integer; average : Array[1..5] of real; i : integer; begin for i := 1 to 5 do    with stdmarkRec[i] do       begin writeln('Enter student name'); readln(stdName); writeln('Enter marks for cat1, cat2 and exam'); readln(cat1, cat2, exam); total[i] := math + eng + chem; average[i] := total[i] / 3; end; writeln; for i := 1 to 5 do       begin writeln('Student Name : ', stdmarkRec[i].stdName); writeln('Total marks : ', total[i]); writeln('Average     : ', average[i]:5:2); end; end.

Arrays within Records
Pascal allows the use of arrays of any data type within records e.g. Type studRec = record stdName: string; scores: Array[1..3] of integer; end; var stdmarkRec : Array[1..2] of studRec; Example: program arrayinrec(input, output); Type studRec = record stdName: string; scores: Array[1..3] of integer; end; var stdmarkRec : Array[1..2] of studRec; total : Array [1..3] of integer; i, j : integer; begin for i := 1 to 2 do       with stdmarkRec[i] do           begin writeln('Enter student Name'); readln(stdName); total[i] := 0; writeln('Enter marks for 3 subjects'); for j := 1 to 3 do               begin readln(scores[j]); total[i] := total[i] + scores[j]; end; end; writeln; writeln('Student Name        Score 1           score 2           score 2        total  '); for i := 1 to 2 do       with stdmarkRec[i] do           begin write(stdName:15); for j := 1 to 3 do                 write(scores[j]:12); writeln(total[i]:12); end; end.

Passing a Record to a procedure
Passing a record to a procedure is similar to passing an Array to a Procedure. In order to pass a record to a procedure, it must be declared through type declaration in the program. Example: program recordpassing(input, output); Type Item = Record itemName : string; manufacturer : string; price : real; end; var itemRec : Item; procedure getItemDetail (var TitemRec : Item); begin with TitemRec do           begin writeln('Enter Item Name'); readln(itemName); writeln('Enter Manufacturer'); readln(manufacturer); writeln('Enter price'); readln(price); end; end; procedure displayItemDetails (TitemRec : Item); begin with TitemRec do           begin writeln('Item Name : ',itemName); writeln('Manufacturer',manufacturer); writeln('Price',price:5:2); end; end; {main program} Begin getItemDetail(itemRec); writeln; displayItemDetails(itemRec); end.

Nested Records
In some cases, a record can also act as an element in another record. Such structures are called "Nested records". Example: program studData(input output); Type dateOfBirth = Record bDate : integer; bMonth : integer; bYear : integer; end; studDetails = Record stdName : string; stdAge : integer; stdBirthdate : dateOfBirth; end; var stdRec : studDetails; Begin with stdRec do      begin writeln('Enter Name'); readln(stdName); writeln('Enter Age'); readln(stdAge); with stdBirthdate do           begin writeln('Enter date'); readln(bDate); writeln('Enter Month'); readln(bMonth); writeln('Enter Year'); readln(bYear); end; end; writeln; writeln('Here is the given information'); writeln('Student Name : ', stdRec.stdName); writeln('Students Age : ', stdRec.stdAge); writeln('Birth Date  : ', stdRec.bDate); writeln('Month Born  : ', stdRec.bMonth); writeln('Year Born   : ', stdRec.bYear); end.

Variant records
This is the property of a record to vary its fields depending on the data being stored.

Syntax: recordname = record declarations; case tagidentifier : datatype of                     label_1: (field declaration); label_2: (field declaration); .                            .                             .                      label_n: (field declaration); end; Example: program studDetails(input, output); var studRec : record studName : string; case option : integer of                   1 : (Maths, Chemistry, Biology : integer); 2 : (Maths, Physics, Chemistry : integer); end; total : integer; begin with studRec do      begin writeln('Enter Name'); readln(studName); writeln('Type 1 for Maths, chemistry / Biology and 2 Maths, Physics / chemistry); readln(option); if (option = 1) then begin writeln('Enter marks for Maths, chemistry and Biology'); readln('Maths, Chemistry, Biology); total := Maths + Chemistry + Biology; end; else begin writeln('Enter marks for Maths, Physics and Chemistry'); readln('Maths, Physics, Chemistry); total := Maths + Chemistry + Physics; end; end; writeln; writeln('Name : ',studRec.studName); writeln('Total : ',total); end.

Files
In pascal, file organization falls into two categories. These are:
 * 1) Sequential file organization.
 * 2) Random file organization.

Sequential organization
The records have to be entered in a proper predefined sequence. The records will then be processed in the same order (sequence) they were entered first in first out (fifo).

It is not possible to process the intermediate records, without disturbing the neighboring records. This methord is better used where all the records need to be processed.

An example is the magnetic tape and cassette recorder.

Random organization
Records are entered in any manner annd you can process any record (makes it possible to process intermediary records without disturbing other records).

Example is the record player.

Pascal further provides basically two types of disc files:
 * 1) Binary files.
 * 2) Text files.