Defining the Type: The process begins by using a keyword like TYPE followed by the record name and a list of field definitions. Each field must have a name and a data type assigned to it before the definition is closed with ENDTYPE.
Declaring Variables: After the type is defined, a variable must be declared using that type. For example, if a type Car is defined, a variable MyCar is declared as an instance of Car to actually allocate memory.
Accessing Fields: Data within a record is accessed using Dot Notation. The syntax follows the pattern , which allows the program to read from or write to a specific attribute within the record instance.
Arrays of Records: To store multiple instances of a record (like a list of 100 students), an array is declared where the base data type is the record type itself. Accessing a specific field in this case requires both an index and a field name: .
| Feature | Record | Array |
|---|---|---|
| Data Types | Heterogeneous (Mixed types) | Homogeneous (Same type) |
| Access Method | Field Name (e.g., .Price) |
Numerical Index (e.g., [5]) |
| Purpose | Representing a single complex entity | Storing a collection of similar items |
| Flexibility | Fixed fields defined at compile time | Fixed size (usually) but uniform elements |
Record vs. Class: While both group data, a record is primarily a data structure used in procedural programming to hold data. A class, used in Object-Oriented Programming, includes both data (attributes) and the functions (methods) that operate on that data.
Field vs. Variable: A field is a variable that exists only within the context of a record. It cannot be accessed independently without referencing the record instance it belongs to.
Check the Hierarchy: When dealing with arrays of records, always ensure the index comes before the dot. A common mistake is writing when the logic requires .
Type vs. Variable: Examiners often test the distinction between defining a type and declaring a variable. Remember that TYPE...ENDTYPE is a template, while DECLARE creates the actual storage space.
Initialization: Always verify that every field in a record is initialized. Because records contain multiple fields, it is easy to forget to assign a value to a boolean flag or a secondary string field, leading to logic errors.
Nested Records: Be prepared for records within records. To access a field in a nested record, you chain the dots: .