pascal define a record
To define a record in Pascal, you can use the record
keyword followed by the name of the record. Inside the record, you can declare various fields or members using the name: type
syntax. Here's an example:
type
TPerson = record
name: string;
age: integer;
address: string;
end;
In the example above, we define a record named TPerson
with three fields: name
of type string
, age
of type integer
, and address
of type string
. You can add as many fields as needed within the record.
Once you have defined a record, you can create variables of that record type and access its fields using the dot notation. Here's an example of creating a variable of the TPerson
record type and accessing its fields:
var
person: TPerson;
begin
person.name := 'John Doe';
person.age := 25;
person.address := '123 Main Street';
writeln('Name: ', person.name);
writeln('Age: ', person.age);
writeln('Address: ', person.address);
end.
In the example above, we create a variable person
of type TPerson
and assign values to its fields. We then print out the values of the fields using the writeln
statement.
That's how you can define and use a record in Pascal. Records are useful for grouping related data together into a single entity.