3 ms·
> 4. A named array is normally passed by reference, as in F(A). But if the argument is not a bare name, as in F((A)), it is passed by value. Visual Basic (VB6/
by ptx 5y ago
> 4. A named array is normally passed by reference, as in F(A). But if
the argument is not a bare name, as in F((A)), it is passed by value.
Visual Basic (VB6/VBA) does exactly this, with the same syntax (except without the outer parentheses if the call is a statement).
- kergonath 5y agoFortran does it as well, I believe. The reason is that (A) is an expression whose value is that of A, and not a variable. This includes Fortran 2018; I am not arguing whether this is modern or not.
- throwawayboise 5y agoIt seems quite reasonable and intuitive to me.
- gnufx 5y agoFortran actually passes by value-return, or whatever you call it, not by reference. The call may still be compiled by reference, of course, and it used to be fun to change the value of a number in the days when it wasn't in read-only storage.
- bregma 5y ago"Fun" in the Dwarf Fortress sense. We once spent days trying to track down an obscure malfunction only to discover that someone had passed a 0 literal as an actual parameter to a certain function that assigned 4 to the formal parameter. For those following along at home, that meant the value of any 0 literal in the program suddenly became 4.
- gnufx 5y agoI don't know what Dwarf Fortress is, but that's what I meant. Fortran now has INTENT specifiers, and you can expect constants to live in .rodata(?) on ELF systems, so you get a SEGV if you evade any compiler warning.
- TheCoreh 5y agoYou can achieve this in JS via `f([...a])` instead of `f(a)`.
- ptx 5y agoIt's not quite the same, as JS doesn't have what's known as pass by reference in VB. JS and Python always pass object references (pointers) by value (the value being the memory address), but in VB you can pass references by reference, references by value, values by reference or values by value, which lets you do things like this: Dim x as Integer x = 1 SomeFunction x Debug.Assert x = 2 ' our local variable was reassigned from elsewhere
- BenFrantzDale 5y agoI believe C++ is getting this (in C++23? I think it was approved…) in the form of if you have `void f(auto&& x)` (taking a universal reference) and call `f(x)` it’s by reference but `f(auto(x))` the `auto(x)` will make a copy so `f` will be passed an rvalue reference.