Records: Grouping Your Data Neatly (Syllabus 3.2.2)
Welcome to the chapter on Records! You've already learned about basic data types like integers and strings, and simple data structures like arrays. But what happens when you need to store complex, related information, like everything about a single student, where each piece of data is a different type?
That’s where Records come in! A Record is an incredibly useful structure that allows you to bundle different types of information together into one neat package. Mastering this concept is key to building well-organised and robust programs. Let's dive in!
What Exactly is a Record?
A Record is a fundamental data structure designed to store a collection of related values, which are known as fields, under a single name. You can then manipulate this entire collection as a single entity.
Analogy: The Student File Folder
Imagine you are keeping physical records for a class. You wouldn't throw all the names, all the grades, and all the birthdates into three separate piles.
Instead, you put all the information related to one student (e.g., Sarah) into one physical file folder labeled "Sarah".
The Record is the file folder (Sarah).
The different bits of information inside are the Fields.
The Key Composition Rule
The most important feature of a record is that its fields typically have different data types.
Look at the fields for our student record:
• Field 1: StudentID (Data Type: Integer)
• Field 2: Name (Data Type: String)
• Field 3: DateOfBirth (Data Type: Date/Time)
• Field 4: IsEnrolled (Data Type: Boolean)
All these different types of data are logically related and grouped into one structure—the Record. The syllabus requires you to be familiar with this composition of a group of values (fields) into a record.
A Record is the container (the entity).
A Field is a slot inside the container that holds one piece of data.
Fields often have different data types.
Records vs. Arrays: Don’t Get Confused!
It’s essential to understand how records differ from arrays (which you learned about in section 3.2.1). This is a common point of confusion for students!
Arrays (Homogeneous Data)
• Purpose: Stores a collection of items that are usually of the same data type (e.g., a list of 50 test scores).
• Access: Items are accessed using an index (position number, e.g., Array[3]).
Records (Heterogeneous Data)
• Purpose: Stores a collection of items that are typically of different data types, but all logically related to one entity (e.g., all the details for Student #50).
• Access: Items are accessed using the Field Name (e.g., StudentRecord.Name).
Memory Aid: Think of an Array as a single-column table of Repeating Rows (same type). Think of a Record as a single Row in a table (different columns/fields).
Working with Records in Programming
In programming, before you can use a Record, you must first define its structure, and then create an instance (a specific copy) of that structure to hold data.
Step 1: Defining the Record Structure
You tell the programming language what the Record will look like—what fields it has and what type of data each field holds.
Let's define a basic record structure called Product:
RECORD Product
ProductID: INTEGER
Name: STRING
Price: REAL/FLOAT
InStock: BOOLEAN
END RECORD
Did you know? In many modern languages like Python, records are often implemented using non-object-oriented classes or data structures like `struct` (C# / VB.Net), purely to bundle data together easily, just as the syllabus mentions.
Step 2: Creating an Entity and Assigning Values
Once the structure is defined, you can create a specific instance of that record (an entity) and assign values to its fields.
We create two entities: Item1 and Item2, both based on the Product structure.
DECLARE Item1 AS Product
DECLARE Item2 AS Product
Now we use dot notation to access the individual fields and assign data:
Item1.ProductID ASSIGN 405
Item1.Name ASSIGN "Laptop"
Item1.Price ASSIGN 899.99
Item1.InStock ASSIGN True
Item2.ProductID ASSIGN 406
Item2.Name ASSIGN "Mouse"
Step 3: Manipulating the Record
Because the record is manipulated as a single entity, you can now pass Item1 (which contains all four related fields) to a subroutine, or store the entire Item1 structure within an array or a list.
For example, to access the price of the first item:
OUTPUT "The price is: " & Item1.Price
(This would display: The price is: 899.99)
This ability to group related, but different, data types makes Records essential for data management and object modelling in computer science.
🔑 Key Takeaway: The Essence of a Record
A Record is a custom-made data package that combines several pieces of logically related data (called fields) into a single unit. Unlike an array, these fields can (and usually do) have different data types.