ParamArray

From Xojo Documentation

Language Keyword

Used in a Sub or Function statement to indicate that an arbitrary number of parameters of the specified data type can be passed.

Usage

ParamArray [parameterName As DataType]

Part Description
parameterName Name of parameter for which an indefinite number of arguments will be passed.
DataType Data type of the parameter. It can be any valid data type.

Notes

The ParamArray keyword enables you to pass an indefinite number of values of a specific data type without formally using an array. A call to a method or function that has been declared using ParamArray uses a comma-delimited list of values rather than an array. The ParamArray parameter is treated as an array in the method code.

ParamArray can only be used with the last parameter in a method.

Sample Code

The following function adds a list of numbers that are passed via ParamArray.

Function AddNumbers(ParamArray nums As Integer) As Integer
Var i, total As Integer
For Each i In nums
total = total + i
Next
Return Total
End Function

This function can now be called like this:

Var sum As Integer
sum = AddNumbers(1, 2, 3, 4, 5)
// sum = 15

Any number of integers can be passed to AddNumbers.

This approach is equivalent to the use of a manually populated array. Consider the following function:

Function AddArray(nums() As Integer) As Integer
Var i, total As Integer
For Each i In nums
total = total + i
Next
Return total
End Function

You can initialize the array and call the function in the following way:

Var myArray(-1) As Integer // declare array without specifying size
Var n As Integer
myArray = Array(5, 10, 20) // create 3 elements and assign values
n = AddArray(myArray)

See Also

Array function; Var, Function, Sub statements.