Executing a sub
You can execute a user-defined sub in either of two ways: by including it in a Call statement or by entering its name followed by the arguments that it expects to be passed (if any). Calling conventions differ according to the number of arguments the sub expects to be passed and whether you use the Call statement to do the calling.
Executing a sub that takes no arguments
When you call a parameterless sub by including it in a Call statement, the sub name can end in either empty parentheses or no parentheses.
For example:
Dim aName As String
Sub PrintName1
' Make the contents of firstName$ be all uppercase
' and display the result.
firstName$ = UCase$(firstName$)
Print firstName$
End Sub
firstName$ = "David"
Call PrintName1()
' Output: DAVID
Call PrintName1
' Output: DAVID
You can call a parameterless sub by entering the sub name, which must not include empty parentheses.
For example:
PrintName1
' Output: DAVID
Executing a sub that takes a single argument
When you call a sub that expects a single argument, enclose the argument in parentheses when you include it in a Call statement. Enclose the argument in single parentheses to pass it by reference, or in double parentheses to pass it by value.
For example:
Sub PrintName2(someName As String)
' Make the contents of someName$ be all uppercase
' and display the result. If someName$'s contents are
' passed by reference, change the value of the
' corresponding variable in the caller's scope.
' Otherwise, don't.
someName$ = UCase$(someName$)
Print someName$
End Sub
firstName$ = "David"
Call PrintName2(firstName$)
' firstName$ is passed by reference by default.
' Output: DAVID
Print firstName$
' Output: DAVID
firstName$ = "David"
Call PrintName2((firstName$))
' Output: DAVID
Print firstName$
' Output: David
You can call a sub that expects a single argument by simply entering the sub's name and the argument. If you enclose the argument in parentheses, it gets passed by value to the sub. For example:
firstName$ = "David"
PrintName2(firstName$)
' firstName$ is passed by value.
' Output: DAVID
Print firstName$
' Output: David
PrintName2 firstName$
' firstName$ is passed by reference.
' Output: DAVID
Print firstName$
' Output: David
Executing a sub that takes multiple arguments
When you call a sub that expects multiple arguments, enclose the arguments in parentheses when you include the sub in a Call statement, and do not enclose them in parentheses when you call the sub by simply entering its name followed by its arguments.
For example:
Dim lastName As String
Sub PrintName3(pronom As String, cognom As String)
pronom$ = UCase$(pronom$)
cognom$ = UCase$(cognom$)
Print pronom$ & " " & cognom$
End Sub
firstName$ = "David"
lastName$ = "LaFontaine"
Call PrintName3(firstName$, lastName$)
Output: ' DAVID LAFONTAINE
firstName$ = "Julie"
lastName$ = "LaFontaine"
PrintName3 firstname$, lastName$
' Output: JULIE LAFONTAINE