function
Try it yourself →Subroutine that returns a value.
Syntax
function name(x: Type): Type; begin name := ...; end;
Description
The return value is assigned to the function name (or Result). It is defined before begin. div is integer division, mod is remainder.
Example
program H;
{$MODE DELPHI}
function square(x: Integer): Integer;
begin
square := x * x;
end;
begin
writeln(square(5));
end.Expected output
25
When to use?
Preventing code repetition.
Common mistakes
- ⚠️ The return value is assigned to the function name: square := x*x; (no return keyword).