Introduction to Records
A record in Pascal groups related values of different types under one name, declared with the 'record ... end' keywords, for example 'type TPlayer = record name: string; age: Integer; average: Real; end;'. Unlike an array, whose elements must all share one type, a record's fields can each have their own type, making it the natural tool for modeling a single real-world entity such as a person, a date, or a product.
Cricket analogy: A record is like a single player's scoreboard entry — name (Rohit Sharma), runs (SR Watson's), strike rate, and dismissal type all live together under one player, unlike a plain array of just runs scored by everyone.
Declaring and Using Fields
Fields of a record are accessed using dot notation, such as 'player.name := 'Kohli'; player.average := 59.3;', and Pascal also provides the 'with' statement to shorten repeated field access within a block, e.g. 'with player do begin name := 'Kohli'; average := 59.3; end;'. Records can be nested inside other records or stored as elements of an array, letting you build structured data like 'var team: array[1..11] of TPlayer;' for a full batting lineup.
Cricket analogy: Using 'with player do' is like a commentator who, once he starts talking about Kohli, just says 'average', 'strike rate', 'centuries' without repeating the player's name each time — the context is already set.
Variant Records
Pascal also supports variant records (also called tagged unions), where a 'case' field inside the record allows different sets of fields depending on a tag value, letting one record type represent several related shapes while sharing common fields. For example, a TShape record might have a common 'color' field followed by 'case kind: TShapeKind of Circle: (radius: Real); Rectangle: (width, height: Real);', so the same variable can hold either a circle's or a rectangle's specific data.
Cricket analogy: A variant record is like a single dismissal-record type that has shared fields (batsman, over) but then a case for 'bowled' (records the ball speed) versus 'caught' (records the fielder's name) — one structure, different extra data depending on how the wicket fell.
type
TPlayer = record
name: string;
matches: Integer;
average: Real;
end;
var
captain: TPlayer;
begin
with captain do
begin
name := 'Rohit Sharma';
matches := 262;
average := 48.6;
end;
writeln(captain.name, ' avg: ', captain.average:0:2);
end.Records can be compared field-by-field only manually in standard Pascal — you cannot write 'if player1 = player2' directly for full records; instead compare individual fields, since the '=' operator is not defined for record types by default.
- A record groups fields of potentially different types under one named structure.
- Fields are accessed with dot notation, e.g. player.name, or shortened with 'with'.
- Records can be nested and stored as elements of an array to model lists of entities.
- Variant records use a 'case' tag field to let one type represent multiple related shapes.
- Standard Pascal does not support direct equality comparison between two record values.
- Records are ideal for modeling one cohesive real-world object, unlike arrays of a single type.
Practice what you learned
1. What keyword pair is used to declare a record type in Pascal?
2. How do you access the 'age' field of a record variable named 'person'?
3. What does the 'with' statement do?
4. What is a variant record used for?
Was this page helpful?
You May Also Like
Arrays in Pascal
Learn how Pascal's fixed-size, strongly-typed arrays work, from simple index ranges to multidimensional matrices.
Pointers in Pascal
Understand Pascal pointers, dynamic memory allocation with New/Dispose, and how they power dynamic structures like linked lists.
Sets in Pascal
Explore Pascal's native set type — union, intersection, difference, and membership testing over ordinal values.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics