Quick start manual
6-14
Delphi Language Guide
Parameters
as a var parameter to another routine. (But when you pass an object reference as a
constant parameter, you can still modify the object’s properties.)
Using const allows the compiler to optimize code for structured- and string-type
parameters. It also provides a safeguard against unintentionally passing a parameter
by reference to another routine.
Here, for example, is the header for the CompareStr function in the SysUtils unit:
function CompareStr(const S1, S2: string): Integer;
Because S1 and S2 are not modified in the body of CompareStr, they can be declared
as constant parameters.
Out parameters
An out parameter, like a variable parameter, is passed by reference. With an out
parameter, however, the initial value of the referenced variable is discarded by the
routine it is passed to. The out parameter is for output only; that is, it tells the
function or procedure where to store output, but doesn’t provide any input.
For example, consider the procedure heading
procedure GetInfo(out Info: SomeRecordType);
When you call GetInfo, you must pass it a variable of type SomeRecordType:
var MyRecord: SomeRecordType;
ƒ
GetInfo(MyRecord);
But you’re not using MyRecord to pass any data to the GetInfo procedure; MyRecord is
just a container where you want GetInfo to store the information it generates. The call
to GetInfo immediately frees the memory used by MyRecord, before program control
passes to the procedure.
Out parameters are frequently used with distributed-object models like COM and
CORBA. In addition, you should use out parameters when you pass an uninitialized
variable to a function or procedure.
Untyped parameters
You can omit type specifications when declaring var, const, and out parameters.
(Value parameters must be typed.) For example,
procedure TakeAnything(const C);
declares a procedure called TakeAnything that accepts a parameter of any type. When
you call such a routine, you cannot pass it a numeral or untyped numeric constant.
Within a procedure or function body, untyped parameters are incompatible with
every type. To operate on an untyped parameter, you must cast it. In general, the
compiler cannot verify that operations on untyped parameters are valid.