Description |
The Procedure keyword defines a subroutine that does not return a value. See the SubRoutines tutorial for details on using procedures.
Version 1
Defines a procedure that operates with no parameters.
Version 2
Defines a procedure that is passed one or more parameters.
When a procedure is defined in a class, it is commonly called a Method.
The same name may be used for more than one procedure 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 procedure as a data type. This allows the procedure to be passed as a parameter, and used as a variable. The type definition defines just the profile of the procedure - and not the name.
A variable of such a type could be assigned the name of any procedure with that profile. When assigned, the variable name can be treated as if it were a procedure 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 |
Function |
|
Defines a subroutine that returns a value |
Out |
|
Identifies a routine parameter for output only |
Overload |
|
Allows 2 or more routines to have the same name |
Override |
|
Defines a method that replaces a virtual parent class method |
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
Procedure ShowSum(a, b : Integer);
var
total : Integer;
begin // Add the two numbers together
total := a + b;
// And display the sum
ShowMessageFmt('%d + %d = %d',[a,b,total]);
end;
// The main form On Create routine - our main program
procedure TForm1.FormCreate(Sender: TObject);
begin // Show the sum of a few number pairs
ShowSum(1,2);
ShowSum(245,62);
end;
end.
|
1 + 2 = 3
245 + 62 = 307
|
|
Example code : Illustrating a Procedure and a Procedure 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
Procedure ShowMsg(msg : string);
begin
ShowMessage(msg);
end;
Procedure ShowLongMsg(msg : string);
begin
ShowMessage('Message text = '+msg);
end;
// Main line code
procedure TForm1.FormCreate(Sender: TObject);
type
TShowProc = Procedure(msg : string);
var
showProc : TShowProc;
begin // Use the ShowMsg proc directly
ShowMessage('Using ShowMsg directly :');
ShowMsg('Hello World');
// Use ShowMsg indirectly
ShowMessage('Using ShowMsg indirectly :');
showProc := ShowMsg;
showProc('Hello again');
// Use ShowLongMsg indirectly
ShowMessage('Using ShowLongMsg indirectly :');
showProc := ShowLongMsg;
showProc('Hello again');
end;
end.
|
Using ShowMsg directly :
Hello World
Using ShowMsg indirectly :
Hello again
Using ShowLongMsg indirectly :
Message text = Hello again
|
|
Example code : Using a type of a procedure 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;
Procedure SetName(name : 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;
// Assigns the passed name
Procedure TSimple.SetName(name : string);
begin
self.name := name;
end;
// Main line code
procedure TForm1.FormCreate(Sender: TObject);
type
TNameProc = Procedure(name : string) of object;
var
simple : TSimple;
nameProc : TNameProc;
begin // Create a simple object
simple := TSimple.Create('Brian');
// And set the name
simple.SetName('New name');
// Show the object name
ShowMessage('Name now = '+simple.GetName);
// Refer to the SetName method
nameProc := simple.SetName;
// And set the name
nameProc('Even newer name');
// Show the object name
ShowMessage('Name now = '+simple.GetName);
end;
end.
|
Name now = New name
Name now = Even newer name
|
|