Passing Arguments to a Function
When a function takes more than one argument, you can pass the arguments to it in any of three ways:
- By supplying the argument values, without their names, positionally (in the order in which the function expects them)
- By supplying the arguments, with their names, in the order in which the function expects them
- By supplying the arguments, with their names, in any order you choose
The first method, supplying the arguments positionally without using their names, is usually the quickest way to proceed. The only disadvantage to doing so is that anyone reading your code may not know immediately which value corresponds to which argument - though they can look this up without trouble. To omit an optional argument, you place a comma where it would appear in the sequence of arguments.
It does take extra time to type in argument names, but it makes your code easier to read. And when you omit an argument from a named argument list, you don't need to use the comma to indicate that you're skipping it.
There's no advantage to using named arguments out of order over using them in order unless you happen to find doing so easier.
For example, the DateSerial function returns a Variant/Date containing the date for the given year, month, and day. The syntax for DateSerial is as follows:
DateSerial(year, month, day)
Here, year is a required Integer argument supplying the year, month is a required Integer argument supplying the month, and day is a required Integer argument supplying the day.
The following statement supplies the arguments positionally without their names:
MsgBox DateSerial(2010, 12, 31)
This statement is equivalent but supplies the arguments positionally with their names:
MsgBox DateSerial(Year:=2010, Month:=12, Day:=31)
The following statement supplies the arguments, with their names, out of order:
MsgBox DateSerial(Day:=31, Year:=2010, Month:=12)
All three of these statements work fine and achieve the same result. You'll cause a problem only if you list out-of-order arguments that you're supplying without names (positionally), if you name some arguments and don't name others, or if you omit required arguments.
In this tutorial:
- Using Built-in Functions
- What Is a Function?
- Passing Arguments to a Function
- Using Functions to Convert Data from One Type to Another
- Using the Asc Function to Return a Character Code
- Using the Str Function to Convert a Value to a String
- Using the Format Function to Format an Expression
- Using the Chr Function and Constants to Enter Special Characters in a String
- Using Functions to Manipulate Strings
- Using InStr and InStrRev to Find a String within Another String
- Using LTrim, RTrim, and Trim to Trim Spaces from a String
- Using Len to Check the Length of a String
- Using the StrComp Function to Compare Apples to Apples
- Using VBA's Mathematical Functions
- Excel VBA's Date and Time Functions
- Using the DateDiff Function to Return an Interval
- Using File-Management Functions