Wednesday, December 10, 2008

Difference between call of Sub/Functional

Actually I was surprised today when I saw this, please check both scenarios:
In one scenario Call addone(i) it transmit I as reference in another one addone(i) - ByVal

Sub Click(Source As Button)
Dim i As Integer
Dim j As Integer
i = 1
For j = 0 To 10
Call addone(i) | addone(i)
Next

Print "i = " & i
End Sub

Sub addone(i As Integer)
i = i + 1
End Sub

4 comments :

Anonymous said...

That's really interesting. I didn't know that.

Great post!

Anonymous said...

Is the same in VBA and VB. I most use:

addone i

Because I don't like the call command.

Anonymous said...

Great example on where you have to be careful how you are passing values.
I have a post with a other angle on the 'Arguments by Value and by Reference' subject
http://idocode.net/idocode/blog.nsf/dx/15-11-2008080118FLKA9E.htm

Yuriy Pastovenskyy said...

Take a look through Lotus Domino Designer Help with a string "Passing arguments by reference and by value"...

There is description inside:

....
Passing by value
You can do the following:

Use the ByVal keyword in the argument's declaration in the function or sub definition.

The argument is passed by value whenever the function or sub is called.

Insert parentheses around the argument in the function or sub call.
Y
ou can control whether an argument is passed by reference or by value at the time when the function or sub is called.

So, it is documented feature.

But, IMHO, it is a stupid thing that reduces code control. My opinion is that you have to make clear declaration of function with 'byval' or without.