Arrays may be passed as arguments to procedures by Val (by value) or by Var (by reference).
The array argument passed to an array Val parameter must have the same number of dimensions and the data-type of the argument must be assignment-compatible with the data-type of the Val parameter. The size of each dimension of the Val parameter is set to match the array argument passed. After this, the array argument is assigned to the array Val parameter (for more details, see Array Assignment). The array Val parameter is a local variable of the called procedure and changes to the array Val parameter during the execution of the procedure have no effect on the array argument which was passed.
The array argument passed to an array Var parameter must have the same number of dimensions and the data-type of the argument must be the same as the data-type of the Var parameter, since the Var parameter is only a temporary alias to the actual array argument passed. Any assignment to the array Var parameter or one of its elements is really an assignment to the corresponding part of the passed array argument. The size of each dimension of the Var parameter is set to match the array argument passed.
The sizeof operator can be used to determine the size of each dimension of the array parameter for the current call. The procedure may require the size of one or more of the dimensions to have a certain size because of the way the procedure is coded and/or the task is supposed to perform. For example, an array Val parameter may be used to transform 3-D data points from one coordinate system to another, in which case the array must be 3 by 3. Alternatively, the procedure may be written to accept any size for the dimensions of the passed argument, but needs to know the upper limit to be used in the loops which process the individual elements of the array. An example of this would be a procedure which accepts an M by N array representing N samples on each of M pins and which calculates the average reading for each pin.
The total number of elements in an array is the sizeof the array divided by the sizeof the data-type of the array. For multiple dimensional arrays, the number of elements in a slice of the array parameter can be calculated and divided into the total number of elements to get the number of slices in the array. For example, if parameter ad is a one-dimensional array of Doubles, the number of elements in the passed argument array is:
Sizeof ad div sizeof Double
and if parameter an is a two-dimensional array of Shorts, the number of elements in a row is:
Sizeof an[0] div sizeof Short
and the number of rows is the total number of elements divided by the number in each row.
Although the sizeof is a prefix operator, the operand can be enclosed in parentheses to make it look like a function call. Also using the real divide operator / instead of the integer divide operator div will still calculate the correct answer, but at the cost of converting the sizes to Float before the division and convert the result back to an integer afterward. Thus the first example above would work correctly if rewritten as:
Sizeof(ad) / sizeof(Double)
The following example shows a procedure with a Val parameter which is a two-dimensional array of Doubles. This M by N array of Doubles is interpreted as N voltage samples on each of M pins and the average voltage for each pin is printed to the log.
Procedure ListPinVoltage( adSamples): Void
------------------------------------------
adSamples: Val Double[ , , ]
dSum: Double
nElements: Short
nPin: Short
nPinCount: Short
nSample: Short
nSamplesPerPin: Short
{
nElements = sizeof adSamples DIV sizeof Double
nSamplesPerPin = sizeof adSamples[0] DIV sizeof Double
nPinCount = nElements DIV nSamplesPerPin
FOR nPin = 0 TO nPinCount-1 DO
dSum = 0.0
FOR nSample = 0 TO nSamplesPerPin -1 DO
dSum = dSum + adSamples[nPin, nSample]
NEXT
PRINT "Pin # "; nPin; " voltage "; dSum/nSamplesPerPin
NEXT
}
Arrays (Definition), Arrays and Structs, Arrays of Variants, Assignment of Arrays, Multiple Dimensions of Arrays, Strings as Arrays