10.1.2 Defining and Using Records
Programs often need several values to describe one real-world item. A record keeps those related values together under one variable, while allowing each field to use the data type that best matches its purpose.
By the end of this section, you should be able to:
- Explain when a record is more suitable than a group of unrelated variables.
- Design a record by identifying suitable fields and data types.
- Write pseudocode to define a record type and declare variables that use it.
- Store, input, read, update and output individual fields using dot notation.
- Recognise how an array of records can store several items with the same structure.
From a Problem Description to a Record
Imagine a wildlife-monitoring program that stores one observation from a camera trap. The observation needs a camera code, a habitat zone, a capture date, the number of animals detected, a confidence score and a review status. These values describe the same observation, so treating them as one structured item makes the program easier to understand.
| Field | Purpose | Example value | Suitable type |
|---|---|---|---|
| CameraCode | Identifies the camera | "CT-N17" | STRING |
| HabitatZone | Stores one zone letter | 'F' | CHAR |
| CapturedOn | Stores the observation date | 18/04/2027 | DATE |
| AnimalCount | Stores a whole-number quantity | 6 | INTEGER |
| ConfidenceScore | Stores a measurement that may include a decimal part | 92.5 | REAL |
| IsReviewed | Records one of two logical states | FALSE | BOOLEAN |
Without a record, a programmer might create separate variables such as
CameraCode1, Zone1, Count1 and Reviewed1.
As more observations are added, keeping the matching values together becomes difficult. A record makes
the relationship explicit.
Define the Record Type
A record type acts as a design for future record variables. The programmer chooses the type name, field names and field data types, so it is a user-defined type. It is also a composite type because it is built from several components.
TYPE WildlifeObservationType
DECLARE CameraCode : STRING
DECLARE HabitatZone : CHAR
DECLARE CapturedOn : DATE
DECLARE AnimalCount : INTEGER
DECLARE ConfidenceScore : REAL
DECLARE IsReviewed : BOOLEAN
ENDTYPE
How to read the declaration
| Part | Purpose |
|---|---|
TYPE WildlifeObservationType |
Starts the definition and names the new record type. |
DECLARE AnimalCount : INTEGER |
Adds a field and states the kind of value it will hold. |
ENDTYPE |
Marks the end of the type definition. |
Declare Record Variables
After the type has been defined, the program can create one or more variables of that type. Every variable receives the same set of fields, but each variable can store different values.
DECLARE LatestObservation : WildlifeObservationType
DECLARE PreviousObservation : WildlifeObservationType
| Name | Role |
|---|---|
WildlifeObservationType |
The type definition or template. |
LatestObservation |
One record variable created from the type. |
AnimalCount |
One field available inside every variable of this type. |
Store, Read and Update Record Fields
A field is accessed using the record variable name, followed by a dot, followed by the field name.
RecordVariable.FieldName
Assigning known values
LatestObservation.CameraCode β "CT-N17"
LatestObservation.HabitatZone β 'F'
LatestObservation.CapturedOn β 18/04/2027
LatestObservation.AnimalCount β 6
LatestObservation.ConfidenceScore β 92.5
LatestObservation.IsReviewed β FALSE
Saving keyboard input into fields
INPUT LatestObservation.CameraCode
INPUT LatestObservation.AnimalCount
INPUT LatestObservation.ConfidenceScore
Each input statement stores the entered value directly in the named field. The input must be compatible with that field's declared data type.
Reading and using fields
OUTPUT LatestObservation.CameraCode
OUTPUT LatestObservation.AnimalCount
IF LatestObservation.IsReviewed = FALSE
THEN
OUTPUT "Review still required"
ENDIF
Updating a field
LatestObservation.IsReviewed β TRUE
LatestObservation.AnimalCount β LatestObservation.AnimalCount + 1
OUTPUT AnimalCount does not identify which record is being used.
Write OUTPUT LatestObservation.AnimalCount when the value is a record field.
Extension: Arrays of Records
One record variable stores one observation. When many observations share the same structure, an array can hold multiple records of the same type. The detailed rules for arrays are covered in Section 10.2, but the combination is useful to recognise here.
DECLARE DailyObservations : ARRAY[1:30] OF WildlifeObservationType
An index selects one record in the array. Dot notation then selects one field inside that record.
DailyObservations[4].CameraCode β "CT-S08"
DailyObservations[4].AnimalCount β 3
DailyObservations[4].IsReviewed β TRUE
OUTPUT DailyObservations[4].CameraCode
DailyObservations[4].AnimalCount from left to right:
select record 4 from the array, then access its AnimalCount field.
Interactive: Record Type Builder
Use the widget to trace the complete process: identify related values, choose field types, define the record type, declare a variable and access its fields.
Common Mistakes and Misconceptions
- Confusing the type with a variable: the type is the design; a variable stores one set of values.
- Assuming every field must use the same data type. Different fields may use different types.
- Leaving out DECLARE, a colon, a field type or ENDTYPE.
- Using only a field name when dot notation is required.
- Assigning a value that does not match the declared field type.
- Grouping values that do not describe the same logical item.
- Trying to access a field that was never included in the type definition.
Practice
Task 1: Design a repair-ticket record
A repair workshop stores a ticket code, the device category, the date received, the estimated cost, whether the repair is urgent and the number of replacement parts required.
- Choose a suitable identifier and data type for each field.
- Write a record type called RepairTicketType.
- Declare a variable called CurrentTicket.
- Write two input statements that save data into fields.
- Set the urgent field to TRUE using an assignment statement.
- Output the estimated-cost field.
Task 2: Explain and correct
- Explain why a record is preferable to six unrelated variables for one repair ticket.
- Correct:
DECLARE CurrentTicket : STRING - Correct:
OUTPUT EstimatedCost - Explain the difference between
RepairTicketTypeandCurrentTicket. - Write one statement that increases the number of required parts by 1.
Review
| Check your understanding | Strong answer |
|---|---|
| Why use a record? | To group related named fields that describe one item, including fields with different types. |
| What is a record type? | A programmer-defined structure that specifies the fields and their data types. |
| What is a record variable? | One instance of the type containing its own field values. |
| How is a field accessed? | With dot notation: RecordVariable.FieldName. |
| How is input saved in a field? | Use the complete field reference, such as INPUT CurrentTicket.TicketCode. |
| What does an array of records store? | Several records that all use the same record type. |