Parameters and Arguments

Consider the following very simple subroutine, which does nothing more than display a message box declaring a person’s name:

	Sub DisplayName(sName As String)
	   MsgBox "My name is " & sName
	End Sub

To call this subroutine, we would write, for example:

	DisplayName "Wolfgang"


	Call DisplayName("Wolfgang")

The variable sName in the procedure declaration:

	Sub DisplayName(sName As String)

is called a parameter of the procedure. In general, it must be filled in when we call the procedure (see the discussion of optional parameters coming soon). The value used in place of the parameter when we make the procedure call is called an argument. Thus, in the previous example, the argument is the string “Wolf-gang”.

Note that many programmers fail to make a distinction between a parameter and an argument, using the names interchangeably. However, since a parameter is like a variable and an argument is like a value of that variable, failing to make this distinction is like failing to distinguish between a variable and its value!

Optional Arguments

In VBA, the arguments to a procedure may be specified as optional, using the Optional keyword. (It makes no sense to say that a parameter is optional; it is assigning its value that is optional.) To illustrate, consider the procedure in Example 6-3, which simply changes the font name and font size of the current selection.

Example 6-3. Using an Optional Argument

Sub ChangeFormatting(FontName As String, Optional FontSize As Variant) ' Change ...

Get Writing Word Macros, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.