Talk:Language Comparison Table: Difference between revisions

(→‎About this page: new section)
Line 74:
 
(I won't modify anything since I am not able to correctly classify any programming lang, so these are just opinions) --[[User:ShinTakezou|ShinTakezou]] 17:06, 1 February 2009 (UTC)
 
== About pass-by-reference ==
 
The terminology of pass-by-value vs. pass-by-reference is really confusing, because different languages and sources use it to mean different things. I propose that we adopt a consistent definition as I will define below.
 
Let's start by considering Java. The consensus is that Java is only pass-by-value (you can search the Internet, official Java forums, etc. and they all agree on this). The argument is that Java has only two types of values -- primitives and references (Java "references" are basically what you would call "pointers" in C, except safer) -- "objects" are not values in Java, you only manipulate them through references (when you create it it returns a reference, the operator "." only operates on references; in short the language has no concept of "objects" as values). And so, often when a beginner claims to be "passing an object", they are actually passing a reference to the object, by value.
 
Why is this not pass-by-reference? Yes, you can modify the object that is pointed to by the reference, and someone else with a reference to the same object will see it; but that is irrelevant. The real thing that you are passing -- the reference -- you cannot somehow modify the caller's copy of. So this is what I consider the defining characteristic of pass-by-reference. If you pass a variable as an argument to a function. And that function is somehow able to effectively perform an assignment to that variable (if the variable is a reference, then this means changing the reference so it points to a different (distinct) object, not modifying the object it points to) as if the assignment was made in the caller.
<lang>
a = somevalue
func(a)
// it had the same effect as if you did
// a = someothervalue
</lang>
 
Under this definition, then, languages like Python and Ruby are also pass-by-value only. In those languages, all values are what Java would call "references" -- different variables can share views of the same object, and variables can be changed to point to different objects without affecting the object it pointed to. And when you pass them, you get the same situation as references in Java. You can still modify the object that the reference points to -- which is shared with the reference passed in -- but you cannot change the caller's reference itself. So you always pass references, by value.
 
C is also pass-by-value only for the same reason. You can pass pointers by value, but that does not count as pass-by-reference. To have a function be able to modify one of your variables, you have to explicitly take the address of it. C++ and PHP's "references" (different from Java's references above), however, do allow pass-by-reference. You just need to declare a parameter of the function to be a reference (with the "&" symbol), and it allows you to modify the caller's variable. The calling code looks just like they are passing a variable; they do not have to explicitly take the address or anything like that, even if that is what the compiler does behind the scenes.
 
One instructive resource is the [[Generic swap]] task. The languages that can perform a swap of two argument variables, without any special caller intervention, like taking the address or putting them in an auxiliary data structure, and affect the caller's variables (instead of just returning the new values), and not doing something like switching the object contents to look like the references to them have been swapped when they really haven't; such languages I would consider pass-by-reference. --[[User:Spoon!|Spoon!]] 20:15, 1 February 2009 (UTC)
Anonymous user