1

I have the following start to a procedure call

strReturn = OrderCreate(strGen8, _
                        strUID, _
                        Order, _
                        boolRecapFlag, _

And on the function that receives the parameters we have..

function OrderCreate(strCoDiv, strUID, byRef Order, boolRecap, strBillFirst, etc.

Since I could not find anywhere where Order values were getting passed into the prodcedure. Am I to assume that the ByRef makes it possible to bring data out of the procedure? Using the Order variable name?

JoJo
  • 4,643
  • 9
  • 42
  • 65

3 Answers3

4

Parameters can be passed into a function in two ways: by reference (ByRef) or by value (ByVal). In VBScript, the default method is ByRef.

When you pass a value by reference, you are passing a reference that variables address in memory. This means that any changes made within your function will persist once your function exists. It can also be used to help control the way your script manages memory since data stored in a variable is only written once in memory.

Passing a parameter by value creates a copy of the variable in memory within the new scope. Changes made to this information within the new scope have no affect on the data in other scopes.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
  • 2
    In VBScript, the default method is **ByRef** ([reference](http://msdn.microsoft.com/en-us/library/ee478101%28VS.84%29.aspx)) – Cheran Shunmugavel Feb 27 '12 at 21:22
  • Protip: To change the handling to ByVal from the calling function side, you can wrap your variable in banana's, so it is evaluated first before it is passed to the function: given function TheAnswer as `Function TheAnswer(v) : v = 42 : End Function` then `v = "Question" : call TheAnswer( v ) ' v becomes 42` but when wrapped: `v = "Question" : call TheAnswer( (v) ) ' v is still "Question"` – AutomatedChaos Feb 28 '12 at 12:29
  • 2
    I mean no disrespect, but that's an amateur tip, not a pro tip. If you enclose v in parenthesis, you are no longer passing a variable into the function. You are passing the result of the expression you've just created. The contents of v remain unaltered because it hasn't been passed ByRef OR ByVal--it hasn't been passed at all! – Nilpo Feb 28 '12 at 14:39
1

byRef means you are passing a reference to the original variable. So if you change the value in the function it is reflected back on the original

This differs from byVal which passes the values and makes an independent copy.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

The difference between byref and byval

dim my_org_age,my_wife_org_age

my_org_age = 43
my_wife_org_age = 43

Call make_our_ages_younger(my_org_age, my_wife_org_age)

After the above sub ( notice it's not even a function! ), my_org_age and my_wife_org_age wil be ten years younger!

That's because the corresponding function here takes its args byREf

sub make_our_ages_younger(byref my_age,byref her_age)
    my_age = my_Age-10
    her_age = her_age-10
end sub

Now, if you remove the "byRef" words out of it, and then run

Call make_our_ages_younger(my_org_age, my_wife_org_age)

you will notice that our ages won't get changed.

See the difference what byRef does now?

HTH

Average Joe
  • 4,521
  • 9
  • 53
  • 81