I think 90% of programming problems are communications problems. Here’s another example of the English language communication problem I mentioned above.
“I want to change my car”. Does that mean “I want to modify the car I have - maybe paint it, or put stiffer suspension in it” or does it mean “I want to sell my car and buy another”?
In the first instance I’m modifying some characteristic of the same car, while in the second I’m replacing the target of the token expression “my car” with an entirely different object. Our language doesn’t provide any formal grammatical way to disambiguate between those two interpretations, and we would rely on common usage to know what is meant.
Context and custom figure heavily in speech: by way of example, and if you will please forgive the sexism, if a woman is heard to say “I want to change my husband”, it will likely be understood - for ugly historical and cultural reasons - to mean a different thing than if a man says “I want to change my wife”. Maybe “I want to change my hair” vs “I want to change my shirt” is a less provocative but equally valid example, and purposeful ambiguation might make for some passable humor. But what if my hair is in the form of a wig? Now what does “change” mean? What if I’m at my tailor, being fitted for a tux? What does “change the shirt” mean now? Alter? Replace?
The comical ambiguities above might easily be resolved by saying “replace”, “modify” or “alter” instead of “change”, but in software it isn’t as easy, because culture doesn’t apply: saying “change what myPtr points to” in a discussion of language behavior could legitimately mean either, yet the difference is monumental, and central to the issue. Personally, in this context I think of “change” and “modify” as in the “hair” example above, and “replace” as in the “shirt” example, but that’s just me.
In the discussions and arguments I’ve seen online, people seem to talk as if the terms are immutable, but they really aren’t, and I haven’t even begun to address the challenge experienced by people for whom English is not the primary language…
So, to add my own “old C programmer” take on this “by value vs. by reference” thing, I think of a function argument as C’s “rvalue”: you cannot change an rvalue, only use the information contained in it. Yes, technically, you can do it locally within the function, of course, but that changes only the local copy, not the “real” data item. The data object to which a pointer refers, however, is an lvalue, and can be changed.
A silly, illegal way of demonstrating the difference might be this:
myInt = new int(5); // yes, I know you can't do that.
then you could pass myInt as a pointer to the new int, and change the original somehow…
void myBumpFunc(int intPtr) {
intPtr.int += 5;
}
you can’t do that, of course…
but you CAN create a single element array of integers…
int[] myInt = new int[1];
“myInt” is a pointer to an array object containing a single integer, and we can pass it to a function to give access to the original:
void myBumpFunc(int[] iP) {
iP[0] += 5;
}
but hey, who’d want to do that? 