A-Level Computer Science / Unit 10: Organising Data in Programs

10.1.2 Defining and Using Records

πŸ”’ Lesson slides are available to signed-in users. Sign in

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
Record: a grouped data item containing named fields that describe one logical entity. Different fields may use different data types.

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.

Exam tip: When justifying a record, mention both ideas: the fields describe the same item, and the fields may require different data types.

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
Field: one named component inside a record. Each field has its own identifier and data type.

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.
Common mistake: Defining a type does not create an observation. It only describes the structure that record variables will follow.

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.
Record variable: one usable instance of a record type, containing its own values for all declared fields.

Store, Read and Update Record Fields

A field is accessed using the record variable name, followed by a dot, followed by the field name.

Dot notation: 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
Common mistake: OUTPUT AnimalCount does not identify which record is being used. Write OUTPUT LatestObservation.AnimalCount when the value is a record field.
Exam tip: Check three things in pseudocode: the record variable is named, the dot is present, and the field exists in the type definition.

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
Read DailyObservations[4].AnimalCount from left to right: select record 4 from the array, then access its AnimalCount field.
An array of records avoids maintaining several separate parallel arrays for values that belong to the same item.

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.

Related values

Library member

Several details describe the same library member, so they can be grouped.

Record type

LibraryMemberType

Step 1 of 5

Identify related fields

Start by deciding which values describe the same logical item.

TYPE LibraryMemberType
    DECLARE MemberName : STRING
    DECLARE MemberID : INTEGER
    DECLARE JoinDate : DATE
    DECLARE BooksBorrowed : INTEGER
    DECLARE IsActive : BOOLEAN
ENDTYPE

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.

  1. Choose a suitable identifier and data type for each field.
  2. Write a record type called RepairTicketType.
  3. Declare a variable called CurrentTicket.
  4. Write two input statements that save data into fields.
  5. Set the urgent field to TRUE using an assignment statement.
  6. Output the estimated-cost field.

Task 2: Explain and correct

  1. Explain why a record is preferable to six unrelated variables for one repair ticket.
  2. Correct: DECLARE CurrentTicket : STRING
  3. Correct: OUTPUT EstimatedCost
  4. Explain the difference between RepairTicketType and CurrentTicket.
  5. 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.
Final exam tip: A complete answer normally needs four precise parts: a valid TYPE...ENDTYPE definition, suitable field types, a record-variable declaration and correct dot notation when reading or storing field values.