Result Variable
Delphi automatically creates a variable named
Result
in every function. The variable’s
type is the return type of the function. The value that
Result
has when the function returns is the
function’s return
value.
In standard Pascal, a function specifies its return value by
assigning to a pseudo-variable whose name is the same as the
function’s. The Result
variable works
similarly, but because it behaves as a real variable, you can use it
in expressions—something you cannot do with the function
name.
Functions return ordinal values, pointers, and small records or sets
in EAX
, and floating-point values on the FPU
stack. Strings, dynamic arrays, Variant
s, and
large records and sets are passed as a hidden var
parameter.
Think of the Result
variable as a
var
parameter. It does not have any valid value
when the function begins, so you must initialize it or otherwise
ensure that it has a meaningful value.
Delphi declares the Result
variable even if you do
not use it. If you try to declare a local variable named
Result
, the compiler issues an error (Identifier
redeclared) message.