Languages Focus: Custom Routines
For non-OOP languages, a custom routine is a function, procedure, or subroutine and for pure OOP languages, a custom routine is a class method. Hybrid languages (both non-OOP and OOP) combine both.
Delphi Prism Custom Routines
In Prism, everything is within a class (just like with C#, VB.Net, and Java). So you create class methods using the method keyword. Alternatively, you can use procedure or function if you want the compiler to enforce returning or not returning a value.
[function/procedure] RoutineName : ReturnType;
As with C++, your custom routine must come before it's first usage or you have to prototype it in the Interface section.
Syntax Example: method Form1.IntroduceYourself;
begin
MessageBox.Show("Hello, I do not have a name yet.");
end;
procedure Form1.SayHello(pName: String);
begin
MessageBox.Show("Hello " + pName);
end;
function Form1.Add(p1, p2 : Double): Double;
begin
Result := p1 + p2;
end;
Old School: Delphi for .Net Step-by-Step Example
In this old-school Delphi for .Net example (before Delphi Prism), notice that you can use global functions and procedures. This was changed with Prism where everything is within a class.
1. Add SysUtils to the uses clause so we can use the VCL.Net IntToStr command.
interface
uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data, Borland.Vcl.SysUtils;
2. Also in the interface section, prototype our two custom routines.
type
//...default form class code here.
procedure SayHello(pName: String);
function Add(a, b : Integer) : integer;
3. Add the two custom routines to the implementation section.
implementation
//...Delphi generated code here.
procedure SayHello(pName: String);
begin
MessageBox.Show('Hello ' + pName);
end;
function Add(a, b : Integer) : integer;
begin
result := a + b;
end;
end.
4. Call the two custom routines from a button click event:
procedure TWinForm.Button2_Click(sender: System.Object; e: System.EventArgs);
begin
SayHello('Mike');
MessageBox.Show(IntToStr(Add(3, 4)));
end;