Evaluated First

It is possible to evaluate the argument before it is passed into a subroutine or function.
This can be achieved by enclosing it inside its own parentheses.
This has nothing to do with the Call keyword.


Calling Subroutines

There are two ways to call a subroutine.
The first is to use the Call keyword in which case the arguments have to appear in parentheses.
The second is to not use the Call keyword in which case the arguments DO NOT have to appear in parentheses.

Call Integer_ByVal(var1) 
Integer_ByVal var1

You can enclose arguments inside there own set of parentheses to force them to be evaluated first.

Call Integer_ByVal((var1)) 
Integer_ByVal (var1)

If you are not using the Call keyword this implicit evaluation is harder to spot.


Example

The following snippets demonstrates that when you enclose your variables in parentheses you get different results.
You should always try and use the Call keyword when you are passing control to a subroutine to avoid confusion.

Sub Integers_AsIntegers() 
   Dim var1 As Integer
   
   var1 = 100
   Call Integer_ByVal(var1)
   Debug.Print var1 '100

   var1 = 100
   Call Integer_ByRef(var1)
   Debug.Print var1 '300
   
'Variables are evaluated first when enclosed in parentheses
   var1 = 100
   Integer_ByRef (var1)
   Debug.Print var1 '100
   
'Variables are evaluated first when enclosed in parentheses
   var1 = 100
   Call Integer_ByRef((var1))
   Debug.Print var1 '100
End Sub

Public Function Integer_ByVal(ByVal var2 As Integer)
   var2 = 200
End Function
Public Function Integer_ByRef(ByRef var2 As Integer)
   var2 = 300
End Function

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext