Pass Arguments to both SubProcedures and Functions (general discussion):
Return to List
Calling Sub Procedures with More than One Argument ' The following example shows two ways to call a Sub procedure with more than one argument. The second time HouseCalc is called, parentheses are required around the arguments because the Call statement is used.
Sub Main()
HouseCalc 99800, 43100
Call HouseCalc(380950, 49500)
End Sub
Sub HouseCalc(price As Single, wage As Single)
If 2.5 * wage <= 0.8 * price Then
MsgBox "You cannot afford this house."
Else
MsgBox "This house is affordable."
End If
End Sub
Using Parentheses when Calling Function Procedures ' To use the return value of a function, assign the function to a variable and enclose the arguments in parentheses, as shown in the following example.
Answer3 = MsgBox("Are you happy with your salary?", 4, "Question 3")
' If you're not interested in the return value of a function, you can call a function the same way you call a Sub procedure. Omit the parentheses, list the arguments, and do not assign the function to a variable, as shown in the following example.
MsgBox "Task Completed!", 0, "Task Box"
' Caution: If you include parentheses in the preceding example, the statement causes a syntax error, because it expects something to receive a value.
Passing Named Arguments ' A statement in a Sub or Function procedure can pass values to called procedures using named arguments. You can list named arguments in any order. A named argument consists of the name of the argument followed by a colon and an equal sign (:=), and the value assigned to the argument.
' The following example calls the MsgBox function using named arguments with no return value. MsgBox Title:="Task Box", Prompt:="Task Completed!"
' The following example calls the MsgBox function using named arguments. The return value is assigned to the variable answer3.
answer3 = MsgBox(Title:="Question 3", _
Prompt:="Are you happy with your salary?", Buttons:=4)'