1. User-Defined Data Types
While programming languages come with built-in (primitive) data types like Integer, Real, and Boolean, they are often not sufficient to model complex real-world data. User-defined data types (UDTs) allow a programmer to create their own data structures. They can be non-composite, representing a single piece of data in a new way, or composite, grouping together existing data types into a new, larger type. This makes code more readable, maintainable, and better at representing the problem being solved.
Key term
Examiner insight
Common pitfall
Worked example 14 marks
A school needs to store data for its students, including their ID (integer), name (string), and average grade (real number). Define a suitable composite user-defined data type in pseudocode to represent a student. Then, declare a variable to store the details of one student.
- 1
- Identify the need for a composite type because multiple different data items (ID, name, grade) need to be grouped for each student.
- 2
- Choose a 'record' or 'structure' as the appropriate UDT. In pseudocode, this is often done using TYPE...ENDTYPE.
- 3
- Define the structure with the specified fields and their corresponding primitive types: StudentID as INTEGER, StudentName as STRING, AverageGrade as REAL.
- 4
TYPE TStudent
- 5
DECLARE StudentID : INTEGER
- 6
DECLARE StudentName : STRING
- 7
DECLARE AverageGrade : REAL
- 8
ENDTYPE
- 9
- Declare a variable of this new type using the DECLARE keyword.
- 10
DECLARE NewStudent : TStudent
Recap
- User-defined types (UDTs) allow programmers to create custom data types.
- Non-composite UDTs, like enumerated types, define a variable that can only take one of a predefined set of values.
- Composite UDTs, like records or classes, group multiple data items together.
- Records are a common composite type for storing related information of different types.
- Using UDTs makes programs easier to understand and manage.
Quick check
- Is a record a composite or non-composite data type? Explain why.2 marks
- Give an example of a non-composite user-defined data type.1 mark