dcsimg
Results 1 to 11 of 11

Thread: Is this right? Sub with Byref can do the same thing a Function does?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Is this right? Sub with Byref can do the same thing a Function does?

    Newbie vb.net learner here. seeking some clarification on this question.
    I think this Sub below

    Sub(ByVal y as integer, Byref X As integer)
    ..
    End sub


    And this function


    Function X (Byval y as integer) as integer
    return x
    end function

    do the same thing in terms of changing the value of X upon completing the sub/function.
    So are these always interchangeable?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,088

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    Passing byRef will change the value of the Passed Variable
    A function returns whatever you tell it to and if you assign the result to the original variable then it will change the original variable. Each has it's uses and they are not really interchangable though could be made to do a similar thing.

    Your code above would not work Your function is passing y and your are returning X which is not the variable passed but the name of the function. I am not sure if this would fail to compile or run in an endless loop but it certianly will not do the same thing as the sub you gave.

    Your Sub has no name so that would not compile either

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,722

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    The former is generally frowned upon, though it is occasionally essential. The reason is more about philosophy than anything else, though. The philosophy is that changing the arguments to a method is a side effect of the method, and side effects make code harder to understand, harder to reuse, and harder to work with. For that reason they are not equivalent in design.

    In the specific case you showed, the Sub may be slower than the Function, simply because it seems like the reference to the integer will mean that the integer has to be boxed and unboxed. However, when I tested the two like so:

    Code:
    Private Sub test1(ByVal arg As Object)
            Dim x As Integer
            Dim st1 As String = ""
            Dim n As Integer
    
    
            For x = 0 To 10000000
                TestVal(x)
            Next
    
        End Sub
    
        Private Sub test2(ByVal arg As Object)
             Dim x As Integer
            Dim st1 As String = ""
            Dim n As Integer
    
    
            For x = 0 To 10000000
                TestRef(x, n)
            Next
    
        End Sub
    
        Private Function TestVal(incoming As Integer) As Integer
            Return incoming + 1
        End Function
    
        Private Sub TestRef(incoming As Integer, ByRef ret As Integer)
            ret += 1
        End Sub
    It turned out that the ByRef version was very slightly faster than the ByVal version. This difference is only about 5% over a million iterations, so it is a vanishingly small amount, but there you have it.
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,088

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    The byRef will make a larger difference on larger variables most noticable on large strings or at least that used to be the case.

    I remember doing some testing on this back in VB5 and values under 10-20 bytes the difference was very small but when those values where several hundred or thousand bytes it became quite noticable at runtime, especially if it was being done often.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,722

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    That should only hold true for Value types, if I understand you correctly....but I think I might not be understanding you correctly. Are you saying that the benefit of using a ByRef will get larger, or that ByRef will get slower, as the size of the item being moved increases?
    My usual boring signature: Nothing

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,088

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    The benefit will increase as the value type gets larger.

    I once took over a project written by someone else where it was parsing a file and it was basically passing the entire file [about 500k] in a string byval to a function. It was doing this in a loop, I forget the details but at any rate when passed byval the routine took about 16 minutes to complete. When changed to byRef it took roughly 16 seconds. So in that case the difference was huge.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,722

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    Strings are weird.
    My usual boring signature: Nothing

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,732

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    If you think about it, it makes sense that performance slows as the byVal object gets bigger... it's more data that has to be copied to the stack - which means a stack allocation has to occur - as opposed to grabbing a pointer and leaving the item on the heap.

    That strings are weird is true indeed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    671

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    Quote Originally Posted by DataMiser View Post
    I once took over a project written by someone else where it was parsing a file and it was basically passing the entire file [about 500k] in a string byval to a function. It was doing this in a loop, I forget the details but at any rate when passed byval the routine took about 16 minutes to complete. When changed to byRef it took roughly 16 seconds. So in that case the difference was huge.
    Passing a string by value does not cause the string to be copied - it's a reference type - there is no copying when passed by value (except the reference itself). Perhaps this was VB6 - can't remember what happened with VB6.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,088

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    The instance I refered to was actually VB5, I am pretty sure that 6 behaves the same but I have no idea about .Net versions. They may be quite different.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,722

    Re: Is this right? Sub with Byref can do the same thing a Function does?

    Strings are semi-reference types. Technically, they are reference types, but they are handled in a special way, so you can cause them to act like value types. Somebody expained that on here once...and I promptly forgot it. The only real key is that you can't be certain that a string will act like other reference types just because it is a reference type.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width