A record is a data type created by the programmer. A record can group variables of different data types and properties together. Lets use a person as an example. There are hundreds of variables that can be used to describe a person, however, this program will use four variables.


The person data type will be created this way:


type

  person = record

                  fname, lname: string;

                  gender: char;

                  wage:real;

                  end;


If we wanted to make a variable employee from the person record we would create the variable like this:


var

  employee:person;




Here is an example program using the person record:


program Project1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


type

  person = record

           fname, lname: string;

           gender: char;

           wage:real;

           end;


var

  employee:person;


procedure StopProgram;

begin

  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln;

end;


Procedure LoadData;

begin

  employee.fname := 'Joe';

  employee.lname := 'Smith';

  employee.gender := 'M';

  employee.wage := 35000.00;

end;


Procedure PrintData;

begin

  writeln('Name: ', employee.fname, ' ' , employee.lname);

  writeln('Gender: ', employee.gender);

  writeln('Wage: $', employee.wage:0:2);

end;


begin

LoadData;

PrintData;

StopProgram;

end.



That example only used four attributes within the person record. There can be hundreds of attributes to describe a person and employee. We differently do not want to keep tying employee.attribute each time we need to input or output data. To make coding easier we can use the command with. Below, is the same program as the previous example, with exception to the with commands.



program RecordExample1w;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


type

  person = record

           fname, lname: string;

           gender: char;

           wage:real;

           end;


var

  employee:person;


procedure StopProgram;

begin

  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln;

end;


Procedure LoadData;

begin

  with employee do

  begin

    fname := 'Joe';

    lname := 'Smith';

    gender := 'M';

    wage := 35000.00;

  end; {with}

end;


Procedure PrintData;

begin

  with employee do

  begin

    writeln('Name: ', fname, ' ' , lname);

    writeln('Gender: ', gender);

    writeln('Wage: $', wage:0:2);

  end; {with}

end;


begin

LoadData;

PrintData;

StopProgram;

end.




A nice thing about the record type is that it can get around the problem of arrays requiring the same data type. This program creates an array, places the data into the array, then prints the data to the screen.


program RecordExample2;


{$mode objfpc}{$H+}


uses

  crt,

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


type

  person = record

           fname, lname: string;

           gender: char;

           wage:real;

           end;


var

  employee:array[1..3] of person;  {example for 3 employees}


procedure StopProgram;

begin

  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln;

end;


procedure EnterData;

var

  x:integer;

begin

  for x := 1 to 3 do

  begin

    with employee[x] do

    begin

      clrscr(); {needs crt added to the units}

      writeln('Employee Number: ', x);

      writeln;

      writeln;

      write('Enter First Name: ');

      readln(fname);

      write('Enter Last Name: ');

      readln(lname);

      write('Enter Gender M/F: ');

      readln(gender);

      write('Enter Wage: $ ');

      readln(wage);

      writeln;

    end; {with}

  end; {for loop}

end; {procedure}


procedure PrintData;

var

  x:integer;


begin

  for x := 1 to 3 do

  begin

    with employee[x] do

    begin

      writeln('Employee Number: ', x);

      writeln('First Name: ', fname);

      writeln('Last Name: ', lname);

      writeln('Gender: ', gender);

      writeln('Wage: $ ', wage:0:2);

      writeln;

    end; {with}

  end; {for loop}

end; {procedure}


begin  {main program}

  EnterData;

  clrscr();

  PrintData;

  StopProgram;

end.












Of course, multiple variables can be created from the same record. This next example uses a record to create two creatures for a roll playing game (RPG).



program rec1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


type

  creature = record

             Name:string;

             Armor, Health, Magic, Power:integer;

             end;



var

  Hero, JellBall:creature;


procedure LoadSettings;   {loads the settings for the hero and monsters}

begin

  with Hero do

  begin

    Name:= 'Sir Bill';

    Armor := 0;

    Health := 10;

    Magic := 2;

    Power := 5;

  end;


  with JellBall do

  begin

    Name:= 'JellBall';

    Armor := 0;

    Health := 1;

    Magic := 0;

    Power := 1;

  end;

end;


procedure PrintSettings;

begin

  with Hero do

  begin

    writeln('Name: ', Name);

    writeln('Armor: ', Armor);

    writeln('Health: ', Health);

    writeln('Magic: ', Magic);

  end;


  writeln;

  writeln;


  with JellBall do

  begin

    writeln('Name: ', Name);

    writeln('Armor: ', Armor);

    writeln('Health: ', Health);

    writeln('Magic: ', Magic);

  end;


end;


procedure StopProgram;

begin

  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln;

end;


begin

  LoadSettings;

  PrintSettings;

  StopProgram;

end.

   


Video Tutorial: Free Pascal Tutorial 13 - On The Record - Lazarus (9:43)

Previous                   Next