Formal parameters are the identifiers that take the values of actual parameters specified during calling of procedures or functions.
Parameters can be passed both by reference and by value. If a formal parameter is described as a parameter by reference, the actual parameter must be a variable or an object field. The description of a formal parameter by reference must be preceded by the Var keyword. If a formal parameter is described as a parameter by value, the actual parameter must be an expression.
The argument passed as a parameter value by reference should have the same type as the parameter type. The argument passed as a parameter value by value should have the same type that can be cast to parameter type.
Formal parameters are local for a procedure or function.
A formal parameter, passed by a value, can be optional. A parameter is considered optional if a default value is defined for it. All the optional parameters must follow in the end of the parameter list. A default value cannot be assigned to a parameter list. To specify a parameter array, use the reserved word Paramarray. The following rules are applicable:
A procedure or function can have only one parameter array and it must be the last argument in the procedure or function signature.
A procedure or function code must process a parameter array as a one-dimensional array, each element of which has the same data type as Paramarray.
A parameters array is always optional. The default value is an empty one-dimensional array with the array element type specified for the parameter array.
All arguments preceding the parameter array in the list must be mandatory. A parameter array must be the only optional argument.
When a procedure or function is called, one should specify their parameters. Two schemes are available to compare formal and actual parameters: positional parameters and named parameters.
When positional parameters are used, actual parameters are formed as a list of parameters separated with commas. Each actual parameter is compared with the formal parameter, to which position it corresponds. In this case optional parameters, to which a default value may correspond, may be used. Then a list of actual parameters should contain mandatory parameters and optional parameters if required. It is not allowed to skip parameters. Therefore, if a procedure has 3 mandatory and 5 optional parameters, and the 7th parameter should be defined, all the 6 previous parameters should be defined as well.
If named parameters are used, actual parameters are formed as a list of pairs: <formal parameter name>:=<actual parameter>. In this case, all mandatory and only the required optional parameters may be specified.
Positional parameters can be combined with named parameters but it is not allowed to specify positional parameters after named parameters.
// a - parameter passed by value
// b - parameter passed by reference
// c - parameter passed by value. Default value = 'a'
Sub MyProc(a: Integer; Var b: String; c: String = "a");
Begin
b := c;
//Set of statements
End Sub MyProc;
// a - parameter passed by value
// b - parameter passed by reference
// c - array of real type statements
Sub MyProc1(a: Integer; Var b: String; Paramarray c: Array Of Double);
Begin
//Set of statements
End Sub MyProc1;
// a, b, c - parameters with default value
Sub MyProc2(a: Integer = 0; b: String = ""; c: Boolean = False);
Begin
//Set of statements
End Sub MyProc2;
// Call with various options of specifying parameters
Sub Run;
Var
params: Array Of Double;
s: String;
Begin
MyProc(10, s); // Only mandatory parameters are specified
MyProc(b := s, a := 10); // Mandatory named parameters are used
MyProc(10, s, "b"); // All parameters are specified
MyProc1(10, s, 1, 2, 3); // All parameters are specified, three last values create an array
params := New Double[3];
params[0] := 1;
params[1] := 2;
params[2] := 3;
MyProc1(10, s, params); // All parameters are specified, three values are passed in the array
MyProc2(1, b := "a"); // One positional and one named parameter
MyProc2(c := True, b := "a", a := 2); // Three named parameters with changing of order specification
End Sub Run;
See also: