This actually was a semester long discussion with a professor of mine 😁.
Here is how I think of it:
Functions are borowed from maths, and in maths functions always copy !
Let's consider the following real function: R->R, f:x->x2
with a=4 if we evaluate f(a)=16 the variable a will still be equal to 4.
In programming(To be clear in most programming languages[1]) functions always copy their arguments, then work the copy. i.e functions are passing_by_value creatures, they always copy what they got, and it's what they copy ("reference type" or "value type") which define if they will act on the state or not.
As @Valentin_Baca pointed out Java behave in the same way, python too :
Python isn't really an exception, but yes, I understand what you're saying. Looking through the python source code, it seems again that it is mostly what we are trying to say when no reference is copied. The re-assignment in python is actually copying a reference, but indeed, no new memory need be allocated, for that reference -- sorta, because the identifier (name) still needs to live... inside the memory.
So even though Python doesn't "copy" the reference like JavaScript does (create a new JSVal that points to the same object), it does so on a waaaay lower level (point directly to the original same object).
Ugh. It's giving me a slight headache 😅😅😅.
However, there are actually quite a few (mostly older) languages that don't copy at all, which would be those languages that are not call by value/sharing/object :).
The most interesting to me are copy-restore languages or those theoretical ones that only copy on write... a topic for another time.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This actually was a semester long discussion with a professor of mine 😁.
Here is how I think of it:
Let's consider the following real function: R->R, f:x->x2
with a=4 if we evaluate f(a)=16 the variable a will still be equal to 4.
In programming(To be clear in most programming languages[1]) functions always copy their arguments, then work the copy. i.e functions are passing_by_value creatures, they always copy what they got, and it's what they copy ("reference type" or "value type") which define if they will act on the state or not.
As @Valentin_Baca pointed out Java behave in the same way, python too :
1: python is an exception, it has mutables and immutables and it passes arguments to functions by assigning them.
Please correct me if I'm wrong
Valentin made a good comment and jmc went into more detail.
Valentin Baca
val_baca valbaca
jmc
johncip http://github.com/johncip/
Python isn't really an exception, but yes, I understand what you're saying. Looking through the python source code, it seems again that it is mostly what we are trying to say when no reference is copied. The re-assignment in python is actually copying a reference, but indeed, no new memory need be allocated, for that reference -- sorta, because the identifier (name) still needs to live... inside the memory.
So even though Python doesn't "copy" the reference like JavaScript does (create a new JSVal that points to the same object), it does so on a waaaay lower level (point directly to the original same object).
Ugh. It's giving me a slight headache 😅😅😅.
However, there are actually quite a few (mostly older) languages that don't copy at all, which would be those languages that are not call by value/sharing/object :).
The most interesting to me are copy-restore languages or those theoretical ones that only copy on write... a topic for another time.