3 ms·
One of the first bits of FORTRAN mischief I learned (on a HP1000 A900) was because values are passed to subroutines by reference, the subroutine could alter an
by anonymousiam 5y ago
One of the first bits of FORTRAN mischief I learned (on a HP1000 A900) was because values are passed to subroutines by reference, the subroutine could alter an argument and the alteration would persist back to the parent(s). This could even be done to constants! Because the HP macro (microcode) architecture did not have immediate arguments, all loads are from initialized memory. Thus if you pass a constant (such as "1") to a subroutine that alters its value, all subsequent use of "1" in the program would no longer be 1.
- sampo 5y agoThe conventional, idiomatic style is to mark the `intent` of your function/subroutine arguments. Then the compiler can check that you don't attempt to mutate input variables, or read the output variables. pure subroutine mean_and_std(vec, mean, std) real, intent(in) :: vec(:) real, intent(out) :: mean, std mean = sum(vec) / size(vec) std = sqrt(sum((vec - mean)**2) / size(vec)) end subroutine
- anonymousiam 5y agoThanks! Apparently the "intent" attribute was added in FORTRAN 2003. This was about 20 years too late for the A900. http://www.hpmuseum.net/display_item.php?hw=594 http://www.hpmuseum.net/display_item.php?hw=594
- sampo 5y ago> Apparently the "intent" attribute was added in FORTRAN 2003. It was added in Fortran 90.
- anonymousiam 5y agoEither way, it was too late for me. The A900 FORTRAN compiler did "Extended FORTRAN 77" and the "card" at the top of each source file was: FTN7X
- gnufx 5y agoFortran (and previously FORTRAN) passes by value-return, not by reference, even if Unix f77 and VAX Fortran implemented it that way. If you use f77(1) in the form of f2c, which I don't have to hand, I changing the value of 1 won't work these days (at least with GCC). It will give a SEGV, because you can expect the C compiler to put constants in read-only storage.
- anonymousiam 5y agoCould you please explain the difference between passing by reference and "value-return"? My understanding is that one can pass by value or by reference. I do not think there is a third alternative. (There are variations on the two in different languages, but I believe there are still only two.) Passing by value creates a copy of the argument and passes the copy. The subroutine/function can modify the copy, but this has no effect on the original argument. Passing by reference is essentially passing a pointer to the object being passed, so if the subroutine/function modifies the object being pointed to, the original value is modified. The latter is what FORTRAN does. MMUs with protected memory, CDS, and no-execute flags are all relatively new concepts that appeared after FORTRAN was created. It's nice that f2c/gcc fixes some of these problems.
- gnufx 5y agoArgument passing is, or was, defined in the "argument association" section of the standard, which obviously doesn't preclude implementing by reference. I don't know whether the same language was in the '66 standard. It isn't anything to do with protected memory as far as I know (which is early '60s); I don't know what CDS is. The point of f2c was just to show that what happens with the invalid code depends on the backend code generation with essentially the same frontend. Regarding different sorts of argument passing, ALGOL 60 had call-by-value, -by-reference, and -by-name.
- anonymousiam 5y agoCDS = code/data separation.