A VERY SIMPLE RECORD
Example program ------> SMALLREC.PAS
We come to the grandaddy of all data structures in Pascal, the record. A record is composed of a number of variables, any of which can be of any predefined data type, including other records. Rather than spend time trying to define a record in detail, lets go right to the first example program, SMALLREC.PAS. This is a program using nonsense data that will illustrate the use of a record.
There is only one entry in the type declaration part of the program, the record identified by the name Description. The record is composed of three fields, the Year, Model, and Engine variables. Notice that the three fields are each of a different type, indicating that the record can be of mixed types. You have a complete example of the way a record is defined before you. It is composed of the identifier Description, the = sign, the reserved word record, the list of elements, and followed by the reserved word end. This is one of the places in Pascal where an end is used without a corresponding begin. Notice that this only defines a type, it does not define any variables. That is done in the var declaration where the variable Truck is defined as a record of type Description and Cars is defined to have 10 complete records of the type Description. The variable Truck has three components, Year, Model, and Engine, and any or all of these components can be used to store data pertaining to Truck.
When assigning data to the variable Truck, for example, there are actually three parts to the variable, so we use three assignment statements, one for each of the sub-fields. In order to assign values to the various sub-fields, the variable name is followed by the sub-field name with a separating period. The "var.sub_field" combination is a variable name.
Keep in mind that Truck is a complete record containing three variables, and to assign or use one of the variables, you must designate which sub-field you are interested in. Examine lines 16 through 18 of the program where the three fields are assigned meaningless data for illustration. The Year field is assigned an integer number, the Model field is assigned the name Pickup, and the Engine variable is assigned the value Diesel.
A loop is then used to assign data to all 10 records of Cars. In order to further illustrate that there are actually 30 variables in use here, a few are changed at random in lines 26 through 30, being very careful to maintain the required types as defined in the type declaration part of the program. Finally, all ten composite variables, consisting of 30 actual variables in a logical grouping are printed out using the same "var.sub-field" notation described above.
If the preceding description of a record is not clear in your mind, review it very carefully. It's a very important concept in Pascal, and you won't have a hope of a chance of understanding the next example until this one is clear. Be sure to compile and run SMALLREC.PAS so you can study the output.
A SUPER RECORD
Example program ------> BIGREC.PAS
Examine the Pascal example file BIGREC.PAS for a very interesting record. First we have a constant defined. Ignore it for the moment, we will come back to it later. Within the type declaration we have three records defined, and upon close examination, you will notice that the first two records are included as part of the definition of the third record. The record identified as Person, actually contains 9 variable definitions, three within the Full_Name record, three of its own, and three within the Date record. Once again, this is a type declaration and does not actually define any variables, that is done in the var part of the program.
The var part of the program defines some variables beginning with the array of Friend containing 50 (because of the constant definition in the const part) records of the user defined type, Person. Since the type Person defines 9 fields, we have now defined 9 times 50 = 450 separate and distinct variables, each with its own defined type. Remember that Pascal is picky about assigning data by the correct type. Each of the 450 separate variables has its own type associated with it, and the compiler will generate an error if you try to assign any of those variables the wrong type of data. Since Person is a type definition, it can be used to define more than one variable, and in fact it is used again to define three more records, Self, Mother, and Father. These three records are each composed of 9 variables, so we have 27 more variables which we can manipulate within the program. Finally we have the variable Index defined as a simple byte type variable.
HOW TO MANIPULATE ALL OF THAT DATA
In the program we begin by assigning data to all of the fields of Self in lines 31 through 43. Examining the first three statements of the main program, we see the construction we learned in the last example program being used, namely the period between descriptor fields. The main record is named Self, and we are interested in the first part of it, specifically the Name part of the Person record. Since the Name part of the Person record is itself composed of three parts, we must designate which component of it we are interested in. Self.Name.First_Name is the complete description of the first name of Self and is used in the assignment statement in line 31 where it is assigned the name of "Charley". The next two fields are handled in the same way and are self explanatory.
WHAT IS THE WITH STATEMENT?
Continuing on to the fourth field, the City, there are only two levels required because City is not another record definition. The fourth field is therefore completely defined by Self.City. Notice the with Self do statement. This is a shorthand notation used with record definitions to simplify coding. From the begin in line 34 to the matching end in line 43, any variables within the Self record are used as though they had a Self. in front of them. It greatly simplifies coding to be able to omit the leading identifier within the with section of code. You will see that City, State, and Zipcode are easily assigned values without further reference to the Self variable. When we get to the Day part of the birthday, we are back to three levels and the complete definition is Self.Birthday.Day but once again, the Self. part is taken care of automatically because we are still within the with Self do area.
To illustrate the with statement further, another is introduced in line 39, with Birthday do, and an area is defined by the begin end pair which extends from line 39 through line 42. Within this area both leading identifiers are handled automatically to simplify coding, and Month is equivalent to writing Self.Birthday.Month if both with statements were removed.
HOW FAR DOWN CAN YOU NEST THE WITH STATEMENT?
You may be wondering how many levels of nesting are allowed in record definitions. There doesn't appear to be a limit according to the Pascal definition, but we do get a hint at how far it is possible to go. In TURBO Pascal, you are allowed to have with statements nested to nine levels, and it would be worthless to nest with statements deeper than the level of records. Any program requiring more levels than nine is probably far beyond the scope of your programming ability, and mine, for a long time.
After assigning a value to Year, the entire record of Self is defined, all nine variables. It should be pointed out that even though Self is composed of nine separate variables, it is proper to call Self a variable itself because it is a record variable.
The statement in line 45, "Mother := Self;" is very interesting. Since both of these are records, both are the same type of record, and both therefore contain 9 variables, Pascal is smart enough to recognize that, and assign all nine values contained in Self to the corresponding variables of Mother. So after one statement, the record variable Mother is completely defined. The statement in line 46 assigns the same values to the nine respective variables of Father, and the next two lines assign all 50 Friend variables the same data. By this point in the program, we have therefore generated 450 + 27 = 477 separate pieces of data so far in this program. We could print it all out, but since it is nonsense data, it would only waste time and paper. Lines 49 through 52 write out three sample pieces of the data for your inspection.
WHAT GOOD IS ALL OF THIS
It should be obvious to you that what this program does, even though the data is nonsense, appears to be the beginning of a database management system, which indeed it is. Instead of assigning nonsense data, a list could be read in and stored for manipulation. It is a crude beginning, and has a long way to go to be useful, but you should see a seed for a useful program.
Now to go back to the const in line 4 as promised. The number of friends was defined as 50 and used for the size of the array and in the assignment loop in line 47. You can now edit this number and see how big this database can become on your computer. You will probably be suprised at how big this number can be.
A VARIANT RECORD
If any part of this chapter is still unclear, it would be good for you to go back and review it at this time. The next example will really tax your mind to completely understand it, and this will be true especially if the prior material is not clear.
Example program ------> VARREC.PAS
Examine the Pascal program VARREC.PAS for an example of a program with a variant record definition. In this example, we first define a scalar type, namely Kind_Of_Vehicle for use within the record. Then we have a record defining Vehicle, intended to define several different vehicles, each with different kinds of data. It would be possible to define all variables for all types of vehicles, but it would be a waste of storage space to define the number of tires for a boat, or the number of propeller blades used on a car or truck. The variant record lets us define the data precisely for each vehicle without wasting data storage space.
WHAT IS A TAG-FIELD?
In the record definition we have the usual record header followed by three variables defined in the same manner as the records in the last two example programs. Then we come to the case statement. Following this statement, the record is different for each of the four types defined in the associated scalar definition. The variable What_Kind is called the tag-field and must be defined as a scalar type prior to the record definition. The tag-field is used to select the variant, when the program uses one of the variables of this record type. The tag-field is followed by a colon and its type definition, then the reserved word of. A list of the variants is then given, with each of the variants having the variables for its particular case defined. The list of variables for one variant is called the field list.
A few rules are in order at this point. The variants do not have to have the same number of variables in each field list, and in fact, one or more of the variants may have no variables at all in its variant part. If a variant has no variables, it must still be defined with a pair of empty parentheses followed by a semi-colon. All variables in the entire variant part must have unique names. The three variables, Wheels, Tires, and Tyres, all mean the same thing to the user, but they must be different for the compiler. You may use the same identifiers again in other records and for simple variables anywhere else in the program. The Pascal compiler can tell which variable you mean by its context. Using the same variable name should be discouraged as bad programming practice because it may confuse you or another person trying to understand your program at a later date.
The final rule is that the variant part of the record must be the last part of it, and in fact, the last part of any or all variants can itself have a variant part to it. That is getting pretty advanced for our level of use of Pascal at this time however.
USING THE VARIANT RECORD
We properly define four variables with the record type Vehicle in line 22 and go on to examine the program itself.
We begin by defining one of our variables of type Vehicle, namely the variable named Ford. The seven lines assigning values to Ford are similar to the prior examples with the exception of line 28. In that line the tag-field which selects the particular variant used is set equal to the value Truck, which is a scalar definition, not a variable. This means that the variables named Motor, Tires, and Payload are available for use with the record Ford, but the variables named Wheels, Engine, Tyres, etc. are not available in the record named Ford.
Next, we will define the record Sunfish as a Boat, and define all of its variables in lines 33 through 41. All of Sunfish's variables are defined but in a rather random order to illustrate that they need not be defined in a particular order. You should remember the with statement from the last example program.
To go even further in randomly assigning the variables to a record, we redefine Ford as having an Engine which it can only have if it is a car. This is one of the fine points of the Pascal record. If you assign any of the variant variables, the record is changed to that variant, but it is the programmers responsibility to assign the correct tag-field to the record, not Pascal's. Good programming practice would be to assign the tag-field before assigning any of the variant variables. The remainder of the Ford variables are assigned to complete that record, the non-variant part remaining from the last assignment.
The variable Mac is now set equal to the variable Sunfish in line 48. All variables within the record are copied to Mac including the tag-field, making Mac a Boat.
NOW TO SEE WHAT WE HAVE IN THE RECORDS
We have assigned Ford to be a car, and two boats exist, namely Sunfish and Mac. Since Schwinn was never defined, it has no data in it, and is at this point useless. The Ford tag-field has been defined as a car, so it should be true in the if statement, and the message in line 51 should print. The Sunfish is not a bicycle, so it will not print. The Mac has been defined as a boat in the single assignment statement, so it will print a message with an indication that all of the data in the record was transferred to its variables.
Even though we can make assignment statements with records, they cannot be used in any mathematical operations such as addition, or multiplication. They are simply used for data storage. It is true however, that the individual elements in a record can be used in any mathematical statements legal for their respective types.
One other point should be mentioned. The tag-field can be completely eliminated resulting in a "free union" variant record. This is possible because Pascal, as you may remember from above, will automatically assign the variant required when you assign data to one of the variables within a variant. This is the reason that all variables within any of the variants must have unique names. The free union record should be avoided in your early programming efforts because you cannot test a record to see what variant has been assigned to it. It is definitely an advanced technique in Pascal.
Be sure you compile and run VARREC.PAS and study the output until you understand it completely.
Advance to Chapter 10
Return to the Table of Contents