Parameters in Delphi

Parameters
Parameters are special kind of variables declared in a subroutine that are used to input some value into that subroutine. And the values that we pass in a function call is called as Arguments. in Delphi we can declare functions/procedures with parameters like following

Example of a Function declaration
    function Add(Num1: integer; Num2: integer) : Integer;
In above function declaration Num1, Num2 is called as Parameters.
Using of function Add...
var
   iResult: integer;
.......
   iResult := Add(10, 20);
10, 20 are called as Arguments that are passed in function call.

We can pass a single or multiple Parameter list to a Delphi subroutine. In case of Parameter list we declare Parameters separated by ";" and enclosed by parenthesis. Parameter names must be valid identifiers. The parameter list specifies the number, order, and type of parameters that must be passed to the routine when it is called.

Within the procedure or function body, the parameter names (Num1 and Num2 in the first example) can be used as local variables.So do not re-declare the parameter names in the local variable declarations section of the procedure or function body. If we do we will get "Identifier Re-declared" compile error.

In Delphi every parameter is classified as value, variable, constant, or out. Value parameters are the default; the reserved words var, const, and out indicate variable, constant, and out parameters, respectively.

Value and Variable Parameters
In Delphi most parameters are either value parameters (the default) or variable (var) parameters. Value parameters are passed by value, while variable parameters are passed by reference. To see the different we can consider following functions:

Example
function DoubleByValue(X: Integer): Integer;   // X is a value parameter
begin
  X := X * 2;
  Result := X;
end;
function DoubleByRef(var X: Integer): Integer;  // X is a variable parameter
begin
  X := X * 2;
  Result := X;
end;

Using of above functions...
var
  I, J, V, W: Integer;
begin
  I := 4;
  V := 4;
  J := DoubleByValue(I);   // J = 8, I = 4
  W := DoubleByRef(V);     // W = 8, V = 8
end;
After this code executes, the variable I, which was passed to DoubleByValue, has the same value we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.

A value parameter acts like a local variable that gets initialized to the value passed in the procedure or function call. If you pass a variable as a value parameter, the procedure or function creates a copy of it; changes made to the copy have no effect on the original variable and are lost when program execution returns to the caller.

A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the parameter within the body of a function or procedure persist after program execution returns to the caller and the parameter name itself has gone out of scope

Out Parameters
An Out parameter is like a variable parameter used for output only. And we should use out parameters when we pass an uninitialized variable to a function or procedure. In case of reference count variables like strings, Interfaces, etc if we declare as Out parameter it always sets default value before executing that Procedure/Function. Generally we use Var and Out parameters to return multiple values from a subroutine.

Example
procedure CopyStr(const AStr: String; out AOutStr: String);
begin
  AOutStr := AStr;
end;

Difference between Var and Out parameters
As we know that both Var and Out parameters have same functionality. So to better understand the difference I have used following example

procedure CopyStr(const AStr: String; out AOutStr: String);
begin
  AOutStr := AStr;
end;
.......
Using of above procedure...
procedure Main();
var
  SomeStr: String;
begin
  SomeStr := 'Hello World';
  CopyStr(SomeStr, SomeStr);
  WriteLn(SomeStr);
  ReadLn;
end.
Normally we would expect the result to be “Hello World“, as nothing basically would happen. AStr is copied to AOutStr which are references to the same variable: SomeStr. But it will result a blank string. Because it initialize AOutStr as blank before entering to CopyStr procedure. Well, if AOutStr was prefixed with a var keyword instead of out then it would result "Hello World".

Constant Parameters
A constant (const) parameter is like a local constant or read-only variable. Constant parameters are similar to value parameters, except that you cannot assign a value to a constant parameter within the body of a procedure or function.

Example
function DoubleVal (const Value: Integer): Integer;
begin
  Value := Value * 2;      // compiler error
  Result := Value;
end;

String Parameters
However we can declare a normal string parameter in a Procedure/Function like we do mostly
function CompareStr(const S1, S2: string): boolean;

But what about declaring a Shortstring parameter of fixed length. 

if we do like following then this will cause a compilation error.
procedure Check(S: string[20]);   // syntax error

But the following declaration is valid:
type TString20 = string[20];
procedure Check(S: TString20);

Array Parameters
When you declare routines that take array parameters, you cannot include index type specifiers in the parameter declarations. 
if we declare an array parameter like follow we will get compile error
procedure Sort(A: array[1..10] of Integer)  // syntax error

But following approach is valid
type TDigits = array[1..10] of Integer;
procedure Sort(A: TDigits);

Or we can use Open Array parameters like following
function Sum (const A: array of Integer): Integer;
Open array parameters allow arrays of different sizes to be passed to the same procedure or function. They are always zero-based. The first element is 0, the second element is 1, and so on. We can get lowest and highest index of an open array by using Low() and High() functions. They cannot be passed to SetLength.
function Sum (const A: array of Integer): Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := Low(A) to High(A) do
    Result := Result + A[I];
end;
Using above function....
var
 X: integer;
 Y: integer;
begin
 Y := 5;
 X := Sum ([10, Y, 27]);
end;

Default Parameters / Optional parameters
We can declare a Parameter as default parameter with a default value and it is optional to pass an argument for a default parameter. So if we don't pass any value for an Default parameter then it will calculate the default value declared as passed value. Default values are allowed only for typed const and value parameters. Parameters with default values must occur at the end of the parameter list.
// is not valid declaration - compilation error //
function MyFunction(X: integer = 5; Y: integer): integer;
// valid declaration //
function MyFunction(X: integer; Y: integer = 5): integer;
....
Using above function..
begin
MyFunction(10);
end;
So when MyFunction will execute it will set X=10 and as we have not passed any value for Y so it will assign default value Y = 5;
begin
MyFunction(10, 20);
end;
But in this case it will set X=10 and Y = 20 as we have passed a value for Y.


Comments

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Drawing Shapes in Delphi