I thought I saw code somewhere do this:
VarX, VarY = SomeFunc(A,B)
Is this possible?
What would the function look like?
Thanks.
Printable View
I thought I saw code somewhere do this:
VarX, VarY = SomeFunc(A,B)
Is this possible?
What would the function look like?
Thanks.
You can do something like this:
Code:Option Explicit
Private Sub Command1_Click()
Dim var1 As String
Dim var2 As Single
DoSomething var1, var2, 9
Debug.Print var1
Debug.Print var2
End Sub
Private Sub DoSomething(Result1 As String, Result2 As Single, someNumber As Integer)
Result1 = "hello"
Result2 = Sqr(someNumber)
End Sub
...or this if you need return value from function as well:
Code:Option Explicit
Private Sub Command1_Click()
Dim var1 As String
Dim var2 As Single
Dim res As Single
res = DoSomething(var1, var2, 9)
Debug.Print var1
Debug.Print var2
Debug.Print res
End Sub
Private Function DoSomething(Result1 As String, Result2 As Single, someNumber As Integer) As Single
Result1 = "hello"
Result2 = Sqr(someNumber)
DoSomething = someNumber ^ 2
End Function
The only thing I can see would be like
What is the purpose? I don't know.Code:Private Sub Form_Load()
Dim VarY As Integer: VarY = 22
Dim VarX As Integer: VarX = 100
Dim B As Integer: B = 8
Dim A As Integer: A = 2
Dim bFlag As Boolean: bFlag = False
Me.AutoRedraw = True
Print VarY, VarX = SomeFunc(A, B)
End Sub
Private Function SomeFunc(ParamA As Integer, ParamB As Integer) As Integer
SomeFunc = 100 'if you change this, the somefunc in the form with print as False
End Function
Here is another little code I cam up with
Code:Private iNumber As Integer
Private Sub Form_Load()
Dim VarY As Integer
Dim VarX As Integer
Dim B As Integer
Dim A As Integer
Dim bFlag As Boolean
Me.AutoRedraw = True
'bFlag will be False because VarX(100) is not the same as iNumber(123)
iNumber = 123
VarX = 9999
bFlag = SomeOtherFunc _
( _
VarY, VarX = SomeFunc(A, B) _
)
Print bFlag
'bFlag will be True because VarX(9999) is the same as iNumber(9999)
iNumber = 9999
VarX = 9999
bFlag = SomeOtherFunc _
( _
VarY, VarX = SomeFunc(A, B) _
)
Print bFlag
End Sub
Private Function SomeFunc(ParamA As Integer, ParamB As Integer) As Integer
SomeFunc = iNumber
End Function
Private Function SomeOtherFunc(ParamA As Integer, ParamB As Integer) As Boolean
If iNumber = 9999 _
Then SomeOtherFunc = True _
Else SomeOtherFunc = False
End Function
Ok. Thanks for all this help.
I learned some things I didn't know.
It looks like you can't multiple assign from a function but the variables you pass into a function are updated by the function.
Thanks
For completeness... A Function can return an Array of values
Also, the variables you pass to a Subroutine or Function can be passed ByVal or ByRef (if you don't specify anything ByRef is the default).Code:Private Function Return4RandomValues() As Integer()
Dim intI As Integer
Dim intTemp(3) As Integer
For intI = 0 To 3
intTemp(intI) = Int(Rnd * 10) + 1
Next intI
Return4RandomValues = intTemp()
End Function
Private Sub Command_Click()
Dim intI As Integer
Dim intA() As Integer
intA = Return4RandomValues()
For intI = 0 To UBound(intA)
Debug.Print intA(intI)
Next intI
End Sub
ByRef means that a reference (effectively the Address of the variable) is passed to the called routine. If the called routine makes any changes to it those changes will be reflected in the Calling routine. ByVal means that a copy of the variable is passed to the called routine and it's scope is Local only to that routine. Any changes made within the called routine will not be reflected in the Calling routine.
A simple example
Clicking on cmdByVal will display the original value of intV in the Immediate Window, clicking on cmdByRef will result in the negated value being displayed.Code:Private Sub aSubroutine(intValue As Integer)
intValue = intValue * (-1)
End Sub
Private Sub bSubroutine(ByVal intValue As Integer)
intValue = intValue * (-1)
End Sub
Private Sub cmdByRef_Click()
Dim intV As Integer
intV = 10
aSubroutine intV
Debug.Print intV
End Sub
Private Sub cmdByVal_Click()
Dim intV As Integer
intV = 10
Call bSubroutine(intV)
Debug.Print intV
End Sub
In aSubroutine, 'intValue' has the same Address in memory as the variable 'intV' in cmdByref. In bSubroutine, 'intValue' has a different Addess in memory and contains a copy of the value of 'intV' in cmdByVal.