Keyword

# ParamArray

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used in a `Sub</api/language/sub>` or `Function</api/language/function>` statement to indicate that an arbitrary number of parameters of the specified data type can be passed.

## Usage

``` xojo
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 <span class="title-ref">ParamArray</span> 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 <span class="title-ref">ParamArray</span> uses a comma-delimited list of values rather than an array. The <span class="title-ref">ParamArray</span> parameter is treated as an array in the method code.

<span class="title-ref">ParamArray</span> 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 <span class="title-ref">ParamArray</span>.

``` xojo
Function AddNumbers(ParamArray nums As Integer) As Integer
  ' nums is treated as an array
  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:

``` xojo
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 as shown below:

``` xojo
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:

``` xojo
Var myArray(-1) As Integer ' create array without specifying size
Var n As Integer
myArray = Array(5, 10, 20) ' assign 3 elements
n = AddArray(myArray)
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Array</api/language/array>` function; `Var</api/language/var>`, `Function</api/language/function>`, `Sub</api/language/sub>` statements.

</div>
