Android Question [b4x] ByRef or Not ByRef - THAT is the question.

MrKim

Well-Known Member
Licensed User
Longtime User
My big takeaway from THIS post is (I think) that regular variables are passed by value and arrays by ref.
I did not know this.
So my question is: What about maps, lists, user defineds, etc.?
And is it always the same across B4A/i/J?
 

MrKim

Well-Known Member
Licensed User
Longtime User
After doing some reading elsewhere I now think I grasp what is going on. For non-primitives a COPY of the reference variable is made and passed to the sub - but it still points to the same ACTUAL value so if you modify the value in the sub you modify the actual value.
Which leads me to another question: What is the value in doing this? With primitives I get it. Passing a copy of the value prevents you from modifying the original variable in the sub. With non-primitives it does not so what does making a copy of the reference buy you?
 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
A primitive value is always stored in a part of the memory that is called stack and has a limited scope i.e. to the current sub. That means it can only be used where is was "dimed". When you pass it to another sub it gets stored in the scope of this sub in the stack memory at a different address. Any manipulation is done at this address, which is different from the first one.

Similar to this is a value of reference type stored in the stack, but it points to a memory address in the so called heap memory. When you pass the reference to another sub, there will be a copy but it still points to the same memory address in the heap. Any manipulation of reference types is made in the heap.

Therefore you are still making a copy of the reference value when you pass it on to the another sub. BUT this copy is only used to access the same address in the heap memory. In conclusion all copies of a reference type point to the same memory address in the heap and manipulation is visible to all.

 
Last edited:
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
See my post #7 above. It is all there if you read it carefully.
@agraham I really appreciate your expertise and contributions to this forum! It's great how you are involved here. Please don't let up in your efforts to explain things. We all benefit greatly from it. Thank you so much!

May I share a short story? I hope it's ok:

In a programming course, the teacher has explained the current topic to the students in detail with slides, several examples and live programming. Then, as usual, he asks, "Are there any questions?". The answer is, "No, no questions." Then he turns to the blackboard to continue with the next topic. Now there begins to be whispering in the room. At first the teacher doesn't notice, but then he hears what is being talked about. The students ask their neighbors if they understood. But they didn't understand it either. So the student in the first row turned around to explain it again to the students in the rows behind. This created unrest and chatter in the classroom. At this point, a strict teacher would call for silence! After all, he explained everything and it was all clear. He could say: Look at the slides and the examples, then everything is clear. (More experienced teachers know, however, that these moments when students explain things to each other are very valuable). Students who don't get it often can't articulate their question at the same level the teacher expects. Therefore, they do not ask at all. Among themselves, however, students find a level at which they can explain it to each other. Often, therefore, when one student in the class asks a question, there are at least five other students who actually had exactly the same question in mind.

When someone asks in this forum because they want to learn something, it's not easy for them. It is so important that we learn from each other. @MrKim Please continue to ask questions.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…