Parameters

A parameter is a variable that is used to pass information into a function. In Intent language, function parameters are passed by value, that is, a copy of each variable is passed to the function; changes to the copied variable are not reflected in the original variable. You can include any number of parameters (separated by commas) in a function or method. The passed parameters are used as variables in statements inside the function.

The Optional Parameter

An optional parameter is declared with the Optional modifier. Parameters that follow an optional parameter in the parameter list must also be Optional or Variable (variable parameters are discussed in the next section). An optional parameter must also specify a default expression for use in the event that no argument is specified.

Function avgTest10(inList As List, Optional limit As Number = 10.0) As Number
   Dim i As Integer
   Dim total As Number = 0.0
   Dim avg As Number  

   For i = 1 To length(inList)
      total = total + nth(i,inList)
   Next i  

   avg =  total / length(inList)

   If avg > limit Then
      printValue("Average = " + stringValue(avg) + " Exceeds limit!")
   Else
      printValue("Average = " + stringValue(avg))
   End If

   Return avg
End Function

In the previous example the average value of a list of numbers is calculated. When the average exceeds a specified limit, a message is printed. The limit is an optional parameter with a default value of 10. To call the function using the default value of the optional parameter, the syntax looks like the following:

To override the value of the optional value limit, you must assign a new value inside the function call (notice the use of the := assignment operator).

Note: The function call does not have to supply optional arguments in the same order as they were defined in the function declaration. However, all required arguments must be supplied first, and must be in the same order as they were defined in the declaration.

The Variable Argument Parameter(...)

A variable parameter is declared with the ellipsis symbol, ... as a modifier. A function may be declared to take any number of parameters. Once any required or optional parameters have been matched, the remaining parameters are collected into a list. That list is supplied as the value of the variable parameter.

There can only be one variable parameter per function, and it must be the last parameter in the declaration. If you omit the ... modifier on an variable parameter, a compile-time error will occur. If an optional type name is specified, it represents the type of each element in the list. If the list contains parameters of various types, the type Any should be used.

Within the body of the function, the variable parameter may be used as a List. If no values have been provided, the list is empty. Thus, the variable parameter list is also optional, with a fixed default value of the empty list.

The following example shows a function that has a single variable parameter of type Number. Arguments supplied from a function call must all be of type Number or Integer.