ByVal v ByRef

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
Office Version
  1. 365
Platform
  1. Windows
It is my understanding that I should use the ByVal keyword if I am
planning to modify the parameter. Is that correct?

Or is it bad form to modify the function parameters at all?

Example:

Public Sub Write2File(byval FileID As String, Data As String)

FileID = FileID & ".txt"

...
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You should use ByVal if you intend to modify the arguments and don't want the changes passed back to the calling routine.
 
Upvote 0
It is my understanding that I should use the ByVal keyword if I am
planning to modify the parameter. Is that correct?
Yes


Or is it bad form to modify the function parameters at all?

Example:

Public Sub Write2File(byval FileID As String, Data As String)

FileID = FileID & ".txt"
...
Personally, I don't think it is bad form at all (although I'm sure there will be others out there who do ;)).
 
Upvote 0
Thanks. I wonder why the default is ByRef? Isn't ByVal better programming practice?

How does ByRef work anyway? If I modify a ByRef parameter, is the variable in the calling routine modified? That sounds dangerous.
 
Upvote 0
Thanks. I wonder why the default is ByRef? Isn't ByVal better programming practice?

How does ByRef work anyway? If I modify a ByRef parameter, is the variable in the calling routine modified? That sounds dangerous.
Again, it depends on what you are doing.

ByRef is the default for a Sub's arguments.
When calling a Function, arguments are passed ByVal as default.
 
Upvote 0
Copy the below code into a VBA module
then run the TestByValAndByRef macro to see the difference:

Code:
Sub TestByValAndByRef()
Dim MyText As String
MyText = "I'll never change"
MsgBox MyText
 
DoSomethingByVal MyParam:=MyText
MsgBox MyText
 
DoSomethingByRef MyParam:=MyText
MsgBox MyText
 
End Sub
 
Sub DoSomethingByVal(ByVal MyParam)
    MyParam = "I've changed"
End Sub
 
Sub DoSomethingByRef(ByRef MyParam)
    MyParam = "I've changed"
End Sub
 
Upvote 0
When calling a Function, arguments are passed ByVal as default.
Not according to VB's help files. From the Function Statement help files...

ByRef - Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.
 
Upvote 0
Not according to VB's help files. From the Function Statement help files...

ByRef - Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.
My goodness, you're right! I guess this just goes to show that one shouldn't trust ones memory of what the defaults are, if its important specify it.

Code:
Sub test()
    Dim testVal As Double
    testVal = 1
    MsgBox DefaultPassing(testVal) & vbCr & testVal
End Sub

Function DefaultPassing(aVal As Double) As Double
    DefaultPassing = aVal
    aVal = aVal + 1
End Function
 
Upvote 0
It's not uncommon (and very convenient) for a function to modify its arguments (passed ByRef) as its primary purpose, and to return just True or False to indicate success.
 
Upvote 0
I guess this just goes to show that one shouldn't trust ones memory of what the defaults are, if its important specify it.
Do you perhaps program in other languages besides VB? It's been a long while since I have, but my recollection is that most other languages pass arguments ByVal by default... VB being contrary in this respect (kind of like its considering True as minus one whereas most other language use plus one for True).
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top