The parameters of a method, if any, are declared by the method's formal parameter list.
formal-parameter-list:
formal-parameters
formal-parameters:
fixed-parameters
fixed-parameters ; parameter-array
parameter-array
fixed-parameters:
fixed-parameter-block
fixed-parameters ; fixed-parameter-block
fixed-parameter-block:
attributesopt parameter-modifieropt parameter-list : type
parameter-list:
identifier
parameter-list , identifier
parameter-modifier:
Var
Out
parameter-array:
attributesopt ParamArray identifier : array-type
The formal parameter list consists of parameter descriptions, of which only the last may be a parameter-array.
A method declaration creates a new declaration space for parameters and local variables.
A method invocation creates a copy, specific to that invocation, of the formal parameters and local variables of the method. Formal parameters are indicated by actual values specified in argument list when invoking the method. Within the body of a method, its parameters can be referenced by identifying their names. A simple name notation is used.
There are four kinds of formal parameters:
Value parameters. Value parameters are declared without any modifiers. A value parameter corresponds to a local variable that gets its initial value from the corresponding argument passed when the method is invoked. The argument passed as the value of a value parameter should be of a type that can be implicitly converted to the parameter type. New values can be assigned to a value parameter within the method body. Such assignments are local and have no effect on the actual argument given in the method invocation.
Reference parameters. A parameter declared with the Var modifier is a reference parameter. A reference parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. An argument in a method invocation must be a variable of the same type as the parameter type.
Output parameters. A parameter declared with the Out modifier is an output parameter. Similar to reference parameters, an output parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. The argument given as the output parameter value must be a variable of the same type as the parameter type. The output parameter value is initially undetermined. It needs to be determined before it is returned from the function.
Parameter-arrays. A parameter declared with the ParamArray modifier is a parameter array and permits declaring a method with a variable number of parameters. A parameter of this type must be the last parameter in the list and it must be of a single-dimensional array type. One of the following ways can be used to give arguments to a method with a variable number of parameters:
The argument given for a parameter array can be a value of the expression of a type that is implicitly convertible to the parameter array type.
Alternatively, the invocation can specify zero or more arguments for the parameter array, where each argument is of a type that is implicitly convertible to the element type of the parameter array. In this case, the invocation creates an array instance containing the given argument values, and uses it as the parameter value.
Except for allowing a variable number of arguments in an invocation, a parameter array is precisely equivalent to a value parameter of the same type.
Class MethodParams
//Parameter i is passed by value
//Parameter j is passed by reference
Public Sub A(i: integer; Var j: integer);
Begin
End Sub;
//Output parameter i
Public Sub B(Out i: integer);
Var
Result: integer;
Begin
//Procedure code
i := Result;
End Sub;
//Parameter-array Arr
Public Sub C(Paramarray Arr: array Of double);
Begin
End Sub;
End Class;
Sub Test();
Var
Obj: MethodParams = New MethodParams();
Result: integer;
Arr: array Of double = New double[3];
Begin
Obj.A(1, Var Result);
Obj.B(Out Result);
Arr[0] := 1;
Arr[1] := 2;
Arr[2] := 3;
Obj.C(Arr);
End Sub;
See also: