Back

Record formats

Introduction
A record is a collection of data items, which usually have a variety of data types. A typical use might be to store a range of details about a pupil, or a range of details about a book. Once you have defined (or 'declared') a record, you can then declare variables to be of that record type. Don't worry if that sounds confusing! Let's look at an example.

Declaring a record
The first thing you would typically do in a language is to define the record. Remember, we are using pseudo-code here. The exact format of the statements will depend on the language you are using.

Type            
Book = Record                                              '
     Title , Author, ISBN : String;            
     Price : Real;                                   
End record;
     
                                

The first line tells the language that we are about to define our own data type (a record). The second line says to call the new data type Book. The next two lines defines what fields the record will have. Title, author and ISBN will be data type string. Price will be data type Real. The new record definition is closed in the last line. We now have a new data type called Book. To use this new data type, we have to declare a variable of that data type. That's what we are doing in the next two lines:

Var
FirstBook : Book;   
    
                                  .    

 Note that if we wanted to store the details about a second book, then we would been to declare an additional variable, perhaps called SecondBook.

Writing data to a record
We can add some code now to write data into each of the fields in the record. The format is this:

VariableName.Field := data     (e.g. FirstBook.Author := "J K Rowling";)

So, to add some details to our record, the code would go something like this:

Type            
Book = Record                                              '
     Title , Author, ISBN : String;            
     Price : Real;                                   
End record;   

Var
FirstBook : Book;

Begin

    FirstBook.Title := "Harry Potter and the Half blood Prince";
    FirstBook.Author := "J K Rowling";
    FirstBook.ISBN := "0747581088";
    Firstbook.Price := 10.99;
End.

You now have some data in the variable Firstbook.

Reading from a record
Of course, from time to time, we will need to get at the data, read from the record. This is straightforward.

Type            
Book = Record                                              '
     Title , Author, ISBN : String;            
     Price : Real;                                   
End record;    
Var
FirstBook : Book;

Begin
    FirstBook.Title := "Harry Potter and the Half blood Prince";
    FirstBook.Author := "J K Rowling";
    FirstBook.ISBN := "0747581088";
    Firstbook.Price := 10.99;

    Writeln('The details of the book are as follows:');
    Writeln;
    Writeln('The book title is ', FirstBook.Title);
    Writeln('The author is ', FirstBook.Author);
    Writeln('The ISBN is ', FirstBook.ISBN);
    Writeln('The price is ', FirstBook.Price);
    Readln;
End.

Now we know how to declare a record. we know that once a record is declared, you have to declare a variable of that record data type. We know the format for writing to a record's fields. We know the format for reading from a record's fields.

Back