Variables Delphi

var Result : Function-Return-Type;


Description
The Result variable is used to hold the result value to be returned by a function.

Delphi automatically creates this variable when the function starts. It is of the same type as the return type of the function.

Result can be used throughout a function as if it were explicitly declared. Whatever its value is upon return from the function is what is passed back.

Result can be seen as equivalent to an out type parameter - where the value upon entry to the function is undefined.

Notes
If the $ExtendedSyntax compiler directive is switched Off, then you must use the old return machanism - set the result of a function to a variable of the same name as the function. Such variables can only be assigned to.


Related commands
$ExtendedSyntax Controls some Pascal extension handling
Function Defines a subroutine that returns a value

Example code : Call a simple function that assigns to Result
begin
// Get the square of some numbers
ShowMessageFmt('Square of %d = %d',[2, SquareIt(2)]);
ShowMessageFmt('Square of %d = %d',[8, SquareIt(8)]);
end;
// Simple function that returns the square of the parameter
function TForm1.SquareIt(value: Integer): Integer;
begin
// Return the value in the Result variable
Result := value * value;
end;

Show full unit code
Square of 2 = 4
Square of 8 = 64