Breaking a program into named routines, and grouping related fields into a record — the two techniques that turn a long script into a structured solution.
A procedure performs an action; a function returns a value. In a function you return by assigning to the special variable Result.
Example
procedure Greet(name: String); begin WriteLn('Hello, ', name, '!'); end; function Area(width, height: Real): Real; begin Result := width * height; end; procedure Swap(var a, b: Integer); // var = by reference var temp: Integer; begin temp := a; a := b; b := temp; end;
| Parameter kind | Written | Behaviour |
|---|---|---|
| By value (default) | (n: Integer) | The routine gets a copy; changes don't escape |
| By reference | (var n: Integer) | The routine works on the caller's variable; changes persist |
| Constant | (const s: String) | By reference for speed, but the routine may not change it |
A record groups related fields under one name. Combined with an array it models a table of data — the standard shape for a CAPS data-processing question.
Example
type TLearner = record Name: String; Grade: Integer; Average: Real; end; var learners: array[1..3] of TLearner; begin learners[1].Name := 'Ayanda'; learners[1].Average := 78.5; end.
💡 Exam Tip
Forgetting to assign to Result means the function returns whatever happened to be in memory — set it on every possible path through the function.