Ruud H.G. van Tol on Tue, 11 Mar 2025 15:27:36 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

calling by reference



The user manual states:

2.7.2 [...] If an argument is prefixed by a tilde ~ in the function declaration and the call, it is passed by reference. (If either the declaration or the call is missing a tilde, we revert to a call by value.)


But:

? vecadd(~v,x)= for(i=1,#v,v[i]+=x);

? {
  my(v=[1..6]); print(v);
  vecadd(v,3); print(v);
  vecadd(~v,5); print(v);
}
[1, 2, 3, 4, 5, 6]
[4, 5, 6, 7, 8, 9]
[9, 10, 11, 12, 13, 14]

So the "function signature" seems to be enough.
(but see below)


See also the documentation of for example listput and listinsert, which mentions using ~L.

I have never noticed a difference, when either using the tilde or not.


But then this is unexpected:

? F(~v) = v[1]++;

? my(v=vector(3)); F(v); print(v)
[1, 0, 0]

? v=vector(3); F(v); print(v)
[0, 0, 0]

-- Ruud