Background
What I want is to make the behaviors of the following two pieces of code identical:
specifier x = getX();
doSomething(x);
function f(specifier x) {
doSomething(x);
}
f(getX());
That is, to make the behavior of passing a parameter identical to initializing a variable. (The initialization looks like an assignment. The relationship between it and assignment is not discussed here. But the distinction is important in the C++ example.)
Beside the type, the specifier could also specify whether x
is a new variable located in an independent address, or an alias of the thing assigned to it, that is, passing by value or by reference. And I may extend it to support more options like the two.
C++ has done it right. Specifically, references could be defined in context other than passing a parameter, to imitate passing a parameter:
Type &x = getX();
Or in C#:
ref Type x = ref getX();
So there is nothing for me to invent. But while I was trying to build something else related to this distinction, I found a strange source of confusion, that some languages tries to make the opposite correspondence. An example is Pascal, where var
means both creating variables in independent addresses, and making alias of the passed parameter:
var x, y : T;
begin
x := y;
doSomething(x); // y is not modified
end.
var y : T;
procedure f(var x : T);
begin
doSomething(x); // y is modified
end;
begin
f(y);
end.
Question
How does languages distinguish passing a parameter by value and reference? I'm especially interested in languages that are not like C++, that initializing a reference isn't so different from assignment, or doesn't have the concept of reference like in C++, so what works in C++ couldn't easily apply. I hope to see languages using the correspondence like in C++, but languages like Pascal or using other ways could also be helpful, as it may tell how the different ideas developed and how much it affects.
Beside the syntax, I'm also interested in how they are distinguished conceptually, in the specification or other materials such as textbooks. The problem of "passing by value or by reference" is it is too specific about parameters. It isn't easily understandable if I say creating a variable, or even creating a nameless value "by value". Languages using keywords might be a bit more helpful than languages using symbols as they automatically provide readable names, but that might not cover the tagless option.