Pass-by-Value vs Reference
Debate on parameter passing semantics in languages like Python, Java, C, and C++, clarifying that they pass object references by value rather than true pass-by-reference.
Activity Over Time
Top Contributors
Keywords
Sample Comments
Why is passing a reference by value so hard to understand?
Python (and Java) do not pass anything by reference. They pass by pointer-value. (If they passed by reference, you could change to which value a caller's variable was bound, like you can in C++).
Python is "pass reference by value", in C++ semantics.
Everything is passed by reference in Python. Everything is passed by value in C.
Java pass by reference vs pass by value vs C pass by reference vs pass by value?
It's not even close, nothing is passed by reference!
I'm still confused and I've been working in various languages for 15 years.If I pass a java object to a function, I'm passing a (probably) 8-byte pointer to some memory with a class tag, fields, whatever. It goes on the stack just like an 8 byte long.In languages that support pointers more directly, I'm doing the same thing, maybe minus the class tag in the pointed-to memory. Address is in an 8 byte type, put it on the stack and access the pointed-to struct in your ne
Calling it "pass by value" without qualifying that, for objects, it passes the reference by value, is very confusing.
Python's official reference might be more appropriate for this. But sometimes, even if things seem similar (e.g. both languages have variables), the semantics might be different. C/C++ has passing by value or by reference. Python and Java has neither, you can mutate the object, but you cannot reassign it (so you can't change the value of an int, but you can change some field of an object.
That's only because strings are immutable in Java. It's not true for reference types in general.In C++ passing a pointer by value is effectively the same as passing a reference, the only real difference being that the syntax for accessing the underlying value is more implicit for a reference.