Results 1 to 8 of 8

Thread: Multiple Assignment from Function?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Multiple Assignment from Function?

    I thought I saw code somewhere do this:


    VarX, VarY = SomeFunc(A,B)

    Is this possible?

    What would the function look like?


    Thanks.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Multiple Assignment from Function?

    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

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Multiple Assignment from Function?

    ...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

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Multiple Assignment from Function?

    The only thing I can see would be like
    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
    What is the purpose? I don't know.

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Multiple Assignment from 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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Re: Multiple Assignment from 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

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Multiple Assignment from Function?

    For completeness... A Function can return an Array of values
    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
    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).

    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
    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
    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.

    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.

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width