Bug? More Documentation Fixes

cklester

Well-Known Member
Licensed User
B4X Basic Language
page 56
Section 5.4.1 For Next

current: In a For–Next loop a same code will be executed a certain number of times.
suggested: In a For–Next loop, the code block will be executed a certain number of times.

Section 5.4.2 For Each
Minor typo (an extra space) in the "Example For - Each:" part:
current: Numbers = Array As Int(1, 3, 5 , 2, 9)
suggested: Numbers = Array As Int(1, 3, 5, 2, 9)

Minor typo (extra space) in the "Example For - Next loop:" part:
current: Numbers = Array As Int(1, 3, 5 , 2, 9)
suggested: Numbers = Array As Int(1, 3, 5, 2, 9)

Page 62, 5.6.5 Parameters says:
In B4X, the parameters are transmitted by value and not by reference.

I'm not sure this is true in all cases. Is it true in this context? Maybe be more specific, as I believe maps, etc., are transmitted by reference. Maybe revise to say:
In B4X, primitive parameters are transmitted by value and not by reference.

Good night!
 

agraham

Expert
Licensed User
Longtime User
I'm not sure this is true in all cases. Is it true in this context?
Yes it is true in all cases. The fact that an instance reference is passed is still a pass by value. Passing by value is defined as passing the contents of a variable whereas pass by reference passes a reference to the variable. The fact that a variable of a type of some object holds a reference means that passing by value passes that reference.
 
Last edited:

cklester

Well-Known Member
Licensed User
Yes it is true in all cases. The fact that an instance reference is passed is still a pass by value. Passing by value is defined as passing the contents of a variable whereas pass by reference passes a reference to the variable. The fact that a variable of a type of some object holds a reference means that passing by value passes that reference.

That last sentence is confusing to me. You seem to be saying that "pass by reference" and "pass by value" are the same or have the same effect.

The docs seem to say that passing by reference and by value are two different things with two different results:

1638307523752.png


If the call to S2() above passed a reference, the value of A would have been 45 after the call. Instead, it is passed by value, so its value does not change.

It's possible I'm confused about how B4X does it. ?
 

cklester

Well-Known Member
Licensed User
Yes it is true in all cases. The fact that an instance reference is passed is still a pass by value. Passing by value is defined as passing the contents of a variable whereas pass by reference passes a reference to the variable. The fact that a variable of a type of some object holds a reference means that passing by value passes that reference.

Oh, I might see what you're saying... Are you saying if I pass by reference to a function, and inside that function, the assigned variable is passed by value, it passes the reference instead of its corresponding value?

That doesn't seem right, though. I have a reference to a value, so that value should be passed on a pass by value.
 

agraham

Expert
Licensed User
Longtime User
You are overthinking this. It is not complicated. My statement above about this is exactly correct, I was careful to phrase it thus.

In the case of a non-primitive type the value of a variable is a refererence to tbe object instance. The point of pass by value is that a called function cannot change the value of the original variable. However if the value is a reference it can modify the object whose reference it is.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
B4X:
Dim varprimitive as Int = 23 ' the value of varparimitive is the integer value 23

Dim varinstance as Object = someobject ' the value of varinstance is a reference to someobject

Sub SomeSub (prim as Int, obj as Object)
' ...
End Sub

' ...
SomeSub(varprimitive, varinstance) ' call SomeSub passing the above variables
' ...

Pass by Value

prim is set to 23, obj is set to the reference to someobject
The value of the local variable prim can be changed but it doesn't affect varprimitive
The properties of someobject can be changed but the object itself in varinstance cannot be changed

Pass by reference - not supported in Java and hence B4X

prim would be set to a reference (memory address) to varprimitive so the contents of varprimitive could be changed by SomeSub
obj would be set to a reference (memory address) to varinstance so the object in varinstance could be changed by SomeSub
 
Last edited:

cklester

Well-Known Member
Licensed User
OK, just to clarify:

In B4X, primitives are passed by value. Therefore, a primitive variable passed to a function will not be changed by the function.

Non-primitives are passed by reference, and the referenced value can be changed by a function.

To me, those are the definitions of passing-by-value and passing-by-reference.

You are further explaining that the reference itself cannot be changed, only the value to which it points. That is, varinstance can't be made to reference a different object.

Now, I had always assumed that, but that distinction makes sense. I can't imagine a language that would let you pass by reference, then let you change what the reference pointed to. Are there languages that do that?!

Anyway, the attached app shows pass-by-value and pass-by-reference examples, confirming all we've discussed herein.
 

Attachments

  • passing_vars_example.zip
    8.8 KB · Views: 191

agraham

Expert
Licensed User
Longtime User
To me, those are the definitions of passing-by-value and passing-by-reference.
Your definitions are wrong. I have clearly explained above what passing by value and reference are according to the accepted definitions of computer science..

Are there languages that do that?!
Yes. C, C++ and C# among many others. Basic4ppc could do it as well!
 

cklester

Well-Known Member
Licensed User
Mind you, I don't have a CS degree, so I could definitely be missing some subtleties... :oops:

Your definitions are wrong. I have clearly explained above what passing by value and reference are according to the accepted definitions of computer science..

Citation needed, because I found a lot of pages that agree with the definition as I currently understand it.

This page agrees with my definitions, it seems. As does this one, and this one, and this one...

In fact, I couldn't find a single page that disagreed with my definitions.

Here is an interesting answer.

How is my definition wrong?
 

cklester

Well-Known Member
Licensed User
Your 'this page' is correct but you are willfully misinterpreting it. I have nothing further to say except you are welcome to wallow in your own ignorance.

How does one "willfully misinterpret" something? Why do you ascribe that motivation to me?

I asked nicely for an explanation! I'm trying to leave my ignorance behind, not wallow in it!

Besides, I'm not ignorant if the entirety of computing science agrees with me... am I?
 

cklester

Well-Known Member
Licensed User
The example app I posted shows that we're both right, I think... Doesn't it? What was wrong with it? Maybe you could add something that would be the definitive source code statement on pass-by-value and pass-by-reference for B4X. ?
 

agraham

Expert
Licensed User
Longtime User
Ok. I apologise for the willfull ignorance bit but you seem intent on not understanding or over-complicating what is a really simple thing.

Those pages you linked to agree with me, and the entirety of computing science. This page in particular that you linked to is exactly what I tries to describe in post #7 above.

Perhaps you are not appreciating that a 'value' of a variable can be a 'reference' to a non-primitive, The value of a variable is its contents, the reference to a variable is its address in memory. Pass by value passes a copy of the value of a given variable (which might be a non-primitive reference), pass by reference passes the variable address. That's all there is to it.
 

cklester

Well-Known Member
Licensed User
Ok. I apologise for the willfull ignorance bit but you seem intent on not understanding or over-complicating what is a really simple thing.

There's something in my personality that strives for precision, and if I feel I don't understand a thing, I tend to ask a lot of questions until I feel like I do understand that thing. It can come off as arguing or nit-picking, but it is truly just a search for the truth, or at least confidence that what I believe is the truth! ?

Those pages you linked to agree with me, and the entirety of computing science.

Then it seems we do accept the same definition for PBV and PBR, we just have expressed it differently.

Perhaps you are not appreciating that a 'value' of a variable can be a 'reference' to a non-primitive,

That is one of the things I learned from this, or at least it became explicit in the definition.

The value of a variable is its contents, the reference to a variable is its address in memory. Pass by value passes a copy of the value of a given variable (which might be a non-primitive reference), pass by reference passes the variable address. That's all there is to it.

Simple! ??
 
Top