Most VB6 procedures—subs and functions—take a fixed number of arguments. The arguments and their types are specified in the procedure definition and, when the procedure is called, the arguments that are passed must precisely match the definition. In some situations, you may want to define one or more optional arguments that can be included or omitted when a procedure is called. You can do so with the Optional keyword.
A procedure can have one or more optional arguments. You must place the optional arguments at the end of the argument list after any non-optional arguments. For example:
Sub foo(A As Integer, B As String, Optional C As Single, Optional D As Date)
You can also specify a default value for an optional argument that will be used if the argument is not passed, like this:
Sub goo(Optional rate As Single = .05)
If the argument rate is passed to the procedure, VB will use the passed value; if it's omitted, VB will use the value .05. If an optional argument is omitted and does not have a default value specified, the VB value for declared but uninitialized variables for that type is used, such as 0 for numeric variables.
Note: You cannot use optional arguments in procedures that use ParamArray arguments.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!