The Delphi Language, Part 2 - Out Parameters, Constant Parameters, and Open Array Parameters
(Page 2 of 14 )
Like var parameters, out parameters provide a means for a routine to return a value to the caller in the form of a parameter. However, although var parameters must be initialized to a valid value prior to calling the routine, out parameters make no assumption about the validity of the incoming parameter. For reference types, this means that the reference will be completely discarded on entering the routine.
procedure ReturnMe(out O: TObject);
begin
O := SomeObject.Create;
end;
Here's a rule of thumb: Use var for in/out parameters and out for out only parameters.
Constant Parameters If you don't want the value of a parameter passed into a function to change, you can declare it with the const keyword. Here's an example of a procedure declaration that receives a constant string parameter:
procedure Goon(const s: string);
Open Array Parameters
Open array parameters provide you with the capability for passing a variable number of arguments to functions and procedures. You can either declare open arrays of some homogenous type or const arrays of differing types. The following code declares a function that accepts an open array of integers:
function AddEmUp(A: array of Integer): Integer;
You can pass to open array routines variables, constants, or expressions of any array type (dynamic, static, or incoming open). The following code demonstrates this by calling AddEmUp() and passing a variety of different elements:
var
I, Rez: Integer;
const
J = 23;
begin
I := 8;
Rez := AddEmUp([I, I + 50, J, 89]);
You can also directly pass an array to an open array routine as shown here:
var
A: array of integer;
begin
SetLength(A, 10);
for i := Low(A) to High(A) do
A[i] := i;
Rez := AddEmUpConst(A);
In order to work with an open array inside the function or procedure, you can use the High(), Low(), and Length() functions to obtain information about the array. To illustrate this, the following code shows an implementation of the AddEmUp() function that returns the sum of all the numbers passed in A:
function AddEmUp(A: array of Integer): Integer;
var
i: Integer;
begin
Result := 0;
for i := Low(A) to High(A) do
inc(Result, A[i]);
end;
The Delphi language also supports an array of const, which allows you to pass heterogeneous data types in an array to a routine. The syntax for defining a routine that accepts an array of const is as follows:
procedure WhatHaveIGot(A: array of const);
You could call the preceding procedure with the following syntax:
WhatHaveIGot(['Tabasco', 90, 5.6, 3.14159, True, 's']);
The compiler passes each element of the array as a System.Object, so it can easily be handled as such in the receiving function. As an example of how to work with array of const, the following implementation for WhatHaveIGot() iterates through the array and shows a message to the user indicating what type of data was passed in which index:
procedure WhatHaveIGot(A: array of const);
var
i: Integer;
begin
for i := Low(A) to High(A) do
WriteLn('Index ', I, ': ', A[i].GetType.FullName);
ReadLn;
end;
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Scope and Units and Namespaces >>
More .NET Articles
More By Xavier Pacheco