Keyword

# Optional

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

## Description

Used in parameter declarations to indicate that a parameter is <span class="title-ref">Optional</span>.

## Usage

``` xojo
Optional parameter As DataType
```

| Part      | Type                | Description                                                |
|-----------|---------------------|------------------------------------------------------------|
| parameter |                     | Parameter that is <span class="title-ref">optional</span>. |
| DataType  | Any valid data type | The data type of the parameter.                            |

## Notes

<span class="title-ref">Optional</span> parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of <span class="title-ref">Optional</span> parameters, it must provide arguments for all preceding <span class="title-ref">Optional</span> parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two <span class="title-ref">Optional</span> parameters.

``` xojo
Sub ExampleMethod(required As Integer, Optional strValue As String, Optional intValue As Integer)
End Sub

' Examples

' Error: if you specify the last optional parameter,
' then you must also specify the one before it
ExampleMethod(3, 4)

' Works: The last parameter is optional
ExampleMethod(3, "test") 

' Works: The last parameters are optional
ExampleMethod(3) 
```

When you use the <span class="title-ref">Optional</span> keyword in a parameter declaration, you make the parameter <span class="title-ref">Optional</span> without specifying a default value. The other way of specifying an <span class="title-ref">Optional</span> parameter is to give it a default value in the declaration. If you omit the <span class="title-ref">Optional</span> parameter when you call such a method, the omitted parameter gets the default value you specified in the declaration.

The <span class="title-ref">Optional</span> keyword precedes the parameter name in the declaration. If the calling statement omits this parameter, it will receive the standard initial value for its type.

## Sample code

If the method declaration is:

``` xojo
myMethod(a As Integer, b As Integer, Optional c As Integer)
```

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

If the call omits the <span class="title-ref">Optional</span> parameter, it will get the default value of 0.

The other way of making the parameter <span class="title-ref">Optional</span> is to give it a default value in the declaration, such as:

``` xojo
myMethod(a As Integer, b As Integer, c As Integer = 10)
```

## Compatibility

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