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 |
|
|
|
Example code : Call a simple function that assigns to Result |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses // The System unit does not need to be defined Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); function SquareIt(value : Integer) : Integer; end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); 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; end.
|
Hide full unit code |
Square of 2 = 4
Square of 8 = 64
|
|