How to explain these two statements?

https://docs.julialang.org/en/v0.6.4/manual/arrays/

In Julia, all arguments to functions are passed by reference.

https://docs.julialang.org/en/v0.6.4/manual/types/#Mutable-Composite-Types-1

An object with an immutable type is passed around (both in assignment statements and in function calls) by copying, whereas a mutable type is passed around by reference.

Because for immutable object there’s no difference.

Let me rephrase what you are saying if you don’t mind

The second statement is correct, and first statement is true only when the argument is immutable
I’m right?

No, first is always right. Second is also right for immutable since you can’t tell the difference.

1 Like

Or put another way: the semantics of argument passing is always by reference. The implementation may be anything as long as it implements reference semantics. For immutables, there’s no way to tell the difference between passing by reference or value, so the compiler is free to choose between them as it sees fit and since passing by value is often the most efficient, that’s generally what it does.

2 Likes

Having it written like that seems slightly confusing. I think it is better that one thinks of all objects being passed exactly in the same way, no matter their mutability, (and then the compiler can of course optimize things behind your back).

Writing it out like that seems to get things the wrong way around. It is the copying that is the optimisation, not the passing by reference.

2 Likes

I can’t tell who you’re replying to…

OP, regarding what is written in the manual:

An object with an immutable type is passed around (both in assignment statements and in function calls) by copying, whereas a mutable type is passed around by reference.

I see that the manual on latest is much better now though:

https://docs.julialang.org/en/latest/manual/types/#Mutable-Composite-Types-1

This is also not quite correct anymore in the current implementation. Large immutables are passed by pointers (to stack memory).

Most responders have commented on the second statement, but I’d like to point out also that the first statement “all arguments to functions are passed by reference” is also potentially misleading. In C++, passing by reference as in void swapint(int& a, int& b) means that the routine can modify scalar variables in its caller: swapint(u,v) changes the caller’s values of u and v. This is not possible in Julia because in Julia, every assignment statement rebinds a variable “from scratch”. However, a Julia newcomer with a C/C++ background and not familiar with Julia’s concept of binding objects to variable names is likely to be confused by the statement “all arguments to functions are passed by reference”.

1 Like

Indeed, and the mentions of reference has been removed from the manual.

https://github.com/JuliaLang/julia/pull/26427
https://github.com/JuliaLang/julia/pull/26447

4 Likes