A subroutine is like a sub-program.
It not only helps divide
your code up into sensible, manageable chunks, but it also allows these chunks
to be used (called) by different parts of your program. Each subroutine contains
one of more statements.
A D V E R T I S E M E N T
In common with other languages, Delphi provides 2 types of subroutine -
Procedures and Functions. Functions are the same as procedures except
that they return a value in addition to executing statements. A Function, as its
name suggests, is like a little program that calculates something, returning the
value to the caller. On the other hand, a procedure is like a little routine
that performs something, and then just finishes.
Parameters to subroutinesBoth functions and procedures can be defined to
operate without any data being passed. For example, you might have a function
that simply returns a random number (like the Delphi
Random
function). It needs no data to get it going.
Likewise, you can have a procedure that carries out some task without the need
for data to dictate its operations. For example, you might have a procedure that
draws a square on the screen. The same square every time it is called.
Often, however, you will pass data, called parameters, to a subroutine. (Note
that the definition of a subroutine refers to parameters as arguments - they are
parameters when passed to the subroutine).
Some simple function and procedure examplesThe following code illustrates
simple function and procedure definitions:
A procedure without parameters
procedure ShowTime;//
A procedure with no parameters
begin
// Display the current date and time
ShowMessage('Date
and time is '+DateTimeToStr
end;
// Let us
call this procedure
ShowTime;
Date and time is 12/12/2002 15:30:45
Notice that we are using some Delphi run time library functions, marked in blue,
in the above code. Click on any to read more.
A procedure with parameters
procedure ShowTime(dateTime :
TDateTime);// With parameters
begin
// Display the date and time passed to the
routine
ShowMessage('Date
and time is '+DateTimeToStr(dateTime));
end;
// Let us
call this procedure
ShowTime(Yesterday);
Date and time is 11/12/2002
function RandomChar : char;
var
i : integer;
begin
// Get a random number from 65 to 90
// (These numbers equate to characters 'A' to
'Z'
i :=
RandomRange(65, 90);
// Return this value as a char type in the
return variable, Result
Result :=
Chr(i);
end;
// Let us
call this function
ShowMessage('Char
chosen is : '+RandomChar);
Char chosen is : A
It is important to note that we return the value from a function in a special
variable called Result that Delphi secretly defines for us to be the same
type as the return type of the function. We can assign to it at any point in the
function. When the function ends, the value then held in Result is then returned
to the caller.
A function with parameters
function Average(a, b, c : Extended) : Extended;
begin
// return the average of the 3 passed numbers
Result :=
Mean(a, b, c);
end;
// Let us
call this function
ShowMessageFmt('Average
of 2, 13 and 56 = %f',[Average(2,13,56)]);
Average of 2, 13 and 56 = 23.67
Interfaces versus ImplementationIn the above examples, we have shown the
subroutines and the calling code in one sequence. In practice, even the calling
code will be in a subroutine, for a very good reason. Let us show complete Unit
code to clarify this:
//
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
Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // Include form definitions
// A small
procedure
procedure InLineProc;
begin
ShowMessage('Hello World');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Call our little in line procedure
InLineProc;
end;
end.
The following is displayed in a little message dialog:
Hello World
The InLineProc we have defined above is literally that - an in-line
subroutine. It must be defined before it is called.
The TForm1.OnCreate procedure is quite different. The TForm1
qualifier gives a clue. This procedure, along with out InLineProc procedure, is
defined in what is called the Implementation section of the Unit. Looking
earlier in the code, you will see a one line declaration of OnCreate in
the Interface part of the Unit. It is part of the class definition for
the form (TForm1) that the Unit and program use as the main screen .
Any subroutine defined in the Interface section must defined in the
Implementation section. Our InLineProc was not, so it needs no advance
declaration.
Data local to a subroutineIn the RandomChar example above, we
declared an integer variable for use in a calculation by the function.
Subroutines can have their own types, constants and variables, and these remain
local to the routine. Variable values are reset every time the routine is called
(use a class object to hold onto data across routine calls). Here is an
illustration of this local variable action:
procedure DoIt(A : Integer);
begin
A := A * 2;
ShowMessageFmt('A in the procedure= %d',[A]);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
A := 22;
ShowMessageFmt('A in program before call = %d',[A]);
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;
in program before call = 22
A in the procedure = 44
A in program now = 22
The procedure is passed A, updates it and displays it. The caller then
displays the A that it passed to the procedure. It is unchanged. The procedure
sees this A as if it were defined as a local variable. Like local variables,
when the procedure ends, their value is lost.
Passing data by referenceThe default was of passing data is by what is
called by value. Literally, the parameter value is passed to the
subroutine argument. reference to the argument is then to this copy of the
variable value.
Passing by reference means that the subroutine actually refers to the passed
variable rather than its value. Any changes to the value will affect the caller
variable. We declare a variable to be passed by reference with the var
prefix. Rewriting the above code to use by reference changes matters:
procedure DoIt
begin
A := A * 2;
ShowMessageFmt('A in the procedure= %d',[A]);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
A := 22;
ShowMessageFmt('A in program before call = %d',[A]);
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;
A in program before call = 22
A in the procedure = 44
A in program now = 44
Now the caller A variable is updated by the procedure.
This is a very useful way of returning data from a procedure, as used by, for
example, the Delphi
Insert routine. It also allows us to return more than one value from a
subroutine.
Output only parametersWe can go further, and define parameters that we
can update, but which are there for update only - output from our subroutine.
They should not be read by the subroutine, the caller not responsible for any
starting value they might contain.
procedure DoIt;
begin
A := 123;
ShowMessageFmt('A in the procedure= %d',[A]);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
A : Integer;
begin
ShowMessage('A before the call is unknown');
// Call the procedure
DoIt(A);
ShowMessageFmt('A in program now = %d',[A]);
end;
A before the call is unknown
A in the procedure = 123
A in program now = 123
Constant value parametersFor code clarity, and performance, it is often
wise to declare arguments that are only ever read by a subroutine as constants.
This is done with the const prefix. It can be used even when a
non-constant parameter is passed. It simply means that the parameter is only
ever read by the subroutine.
procedure DoIt: Integer; Out B : Integer);
begin
B := A * 2;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
A, B : Integer;
begin
A := 22;
// Call the procedure
DoIt(A, B);
ShowMessageFmt('B has been set to = %d',[B]);
end;
B has been set to 44
Notice that when defining two argument types, the arguments are separated with a
Same routine, different parametersOne of the benefits of Object Oriented
programming is that some of the rigidity of procedural languages was relaxed.
This has spilled over into non object orientation subroutines (as opposed to
class methods).
One of the benefits is that we can define two or more subroutines that have
exactly the same name. Delphi is able to tell them apart by the different number
or types of parameters.
The example below illustrates this with two versions of the DoIt
procedure.
procedure DoIt; overload;
begin
ShowMessage('DoIt with no parameters called');
end;
procedure DoIt(msg : String); overload;
begin
ShowMessage('DoIt called with parameter : '+msg);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Call the procedure using no parameters
DoIt;
// Now call the procedure using one parameter
DoIt('Hi there');
end;
vDoIt with no parameters called
DoIt called with parameter : Hi There