Mega Code Archive

 
Categories / Delphi / Keywords
 

Function - defines a subroutine that returns a value

1 Function Name : Return type; {Directives;} 2 Function Name(Parameters) : Return type; {Directives;} 3 type Name = Function{(Parameters)} : Return type {of object}; Description The Function keyword defines a subroutine that returns a value. See the SubRoutines tutorial for details on using functions. Version 1 Defines a function that simply returns a data type. Only one value can be returned from a function. Version 2 Defines a function that is passed one or more parameters, and returns a data type. Only one value can be returned from a function. However, using the out or var keyword before a parameter allows the parameter to be treated as variable to contain return values. In both cases, the returned value is passed by assigning to the Result pseudo variable. Delphi creates this variable for you at the function start, with the correct return data type. (Older Pascal code assigned to a variable with the same name as the function). When a function is defined in a class, it is commonly called a Method. The same name may be used for more than one function as long as the Overload directive is used. The other main directives, in the order that they should appear is given here: Reintroduce : Redefines a suppressed function Overload : Allows same name for 2 or more functions Virtual : Can be redefined in a child class Override : Redefines a parent class method Abstract : Forces child class to implement Version 3 Defines a function as a data type. This allows the function to be passed as a parameter, and used as a variable. The type definition defines just the profile of the function - and not the name. A variable of such a type could be assigned the name of any function with that profile. When assigned, the variable name can be treated as if it were a function name. See the example code. Further still, the Of Object option allows you to refer to an object method. Access to a variable of such type would then behave as if you were calling the object method directly. See the second example. Related commands Abstract Defines a class method only implemented in subclasses Const Starts the definition of fixed data values Out Identifies a routine parameter for output only Override Defines a method that replaces a virtual parent class method Procedure Defines a subroutine that does not return a value Result A variable used to hold the return value from a function Var Starts the definition of a section of data variables Virtual Allows a class method to be overriden in derived classes Dynamic Allows a class method to be overriden in derived classes Example code : A simple example // 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); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions Function GetSum(a, b : Integer) : Integer; begin // Add the two numbers together, and return this value Result := a + b; end; // The main form On Create routine - our main program procedure TForm1.FormCreate(Sender: TObject); var total : Integer; begin // Show the sum of a few number pairs total := GetSum(1,2); ShowMessageFmt('%d + %d = %d',[1,2,total]); total := GetSum(62,444); ShowMessageFmt('%d + %d = %d',[62,444,total]); end; end. 1 + 2 = 3 62 + 444 = 506 Example code : Illustrating a function and a function type // 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 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type // The form class itself TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // In line functions Function MaxValue(a, b : Integer) : Integer; begin // Return the highest of 2 numbers if a > b then Result := a else Result := b; end; Function MinValue(a, b : Integer) : Integer; begin // Return the lowest of 2 numbers if a < b then Result := a else Result := b; end; // Main line code procedure TForm1.FormCreate(Sender: TObject); type TNumberFunc = Function(a, b : Integer) : Integer; var numberFunc : TNumberFunc; a, b, c : Integer; begin // Use the MaxValue function directly a := 3; b := 6; c := MaxValue(a, b); ShowMessage('Direct call to MaxValue :'); ShowMessageFmt('Max of %d and %d is %d',[a,b,c]); // Now call it indirectly numberFunc := MaxValue; c := numberFunc(a, b); ShowMessage('Indirect call to MaxValue :'); ShowMessageFmt('Max of %d and %d is %d',[a,b,c]); // And call it again for the MinValue function numberFunc := MinValue; c := numberFunc(a, b); ShowMessage('Indirect call to MinValue :'); ShowMessageFmt('Min of %d and %d is %d',[a,b,c]); end; end. Direct call to MaxValue : Max of 3 and 6 is 6 Indirect call to MaxValue : Max of 3 and 6 is 6 Indirect call to MinValue : Min of 3 and 6 is 3 Example code : Using a type of a function of a class // 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 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type // Define a simple class TSimple = class private name : string; public function GetName : string; constructor Create(name : string); end; // The form class itself TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Create a simple object constructor TSimple.Create(name: string); begin // Save the passed string self.name := name; end; // Returns the simple name function TSimple.GetName: string; begin Result := name; end; // Main line code procedure TForm1.FormCreate(Sender: TObject); type TNameFunc = Function : string of object; var simple : TSimple; nameFunc : TNameFunc; begin // Create a simple object simple := TSimple.Create('Brian'); // Show the object name ShowMessage('Name accessed directly = '+simple.GetName); // Now refer to this method indirectly nameFunc := simple.GetName; // Show the object name ShowMessage('Name accessed indirectly = '+nameFunc); end; end. Name accessed directly = Brian Name accessed indirectly = Brian