Re: how to use %val() ???



Richard Maine wrote:
....
> It is an oft repeated falacy that Fortran arguments are always
> passed by reference. It is, however, a falacy, at least with the
> word "always" instead of something like "often".

In fact, whereas C implementations are *required* to
conform to pass by reference semantics, few Fortran
implementations even bother to try to conform to such
limitations. Even if the implementation is ostensibly
"call by reference", local copies are often made within
the procedure.

Consider the following illegal Fortran code fragment:

A=1.0
Call sub1(A,A)
....
Subroutine sub1(X,Y)
Real :: X, Y
X = 5.0
Y = X+Y
X = X*Y
Print *, X, Y
End subroutine sub1

This is illegal since Fortran prohibits actual arguments
from being the same, overlapping, or otherwise aliased
if the corresponding dummy arguments are defined within
the procedure or anything the procedure calls in turn.

But, the corresponding C code is *not* prohibited. It
has a well defined meaning in C: the program will
output 100.0 and 10.0. On return, the C actual
argument will have value 100.0. (Note I'm assuming
that the corresponding C was written with pointer
arguments - if not, it's hardly a corresponding code.)

In spite of this being illegal, most Fortran
implementations won't detect the error and will
permit the call. The result *might* be 100.0 and
10.0, but the result also might often be 50.0 and 10.0,
or 36.0 and 6.0, or 30.0 and 6.0, or 25.0 and 5.0, etc.
The result 30.0 and 6.0 is consistent with call by
value/result semantics (if the compiler keeps both
X and Y in registers for the duration of the call, this
is that you get). The Fortran standard doesn't
*require* any of these (the call is illegal). The
code sequences that lead to these are all legal if
the arguments are not aliased. The usual rule
of thumb is that if you can tell the difference
between call by reference and call by value/result
in your code, your code is not standard.

On return, the value of A can be any of the numbers
seen above: it just depends on what order the copies
of X anY are stored into the address of the actual
arguments.

All these alternate values are generated faster
than the corresponding C90 code could manage
since the C versions must store immediately after
each assignment and reload the arguments prior to
the next assignment. With the C99 standard, programmers
can use the 'restrict' attribute and get more efficient
code - but the rules for 'restrict' are very similar
to Fortran's rules for argument association. Fortran
can get C's conventional semantic restrictions by
passing POINTERs everywhere. The targets two
POINTERs (of the same type) must be treated as
possibly aliased in Fortran (F90 or more recent).

All this is not just pedantry. If you notice that
your Fortran implementation appears to always
"pass by reference", you must still not rely on
call by reference semantics. If you do, you are
likely to end up with the worst kind of program
error: the undiagnosed production of plausible
wrong answers.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


.



Relevant Pages

  • Re: call by reference
    ... Some regulars have served on the ISO Fortran standard committee, and at least one is the author of a popular ... It also has a footnote that says some implementations of FORTRAN store literals in a data region so that if you pass a literal and change it within the function you may actually give the literal a new value! ... I found one reference that said before 77, FORTRAN was pass by reference and starting with 77 "scalar variables are often passed by value-result". ...
    (comp.lang.java.programmer)
  • Re: Simple Input File Parsing Question
    ... I am pretty new to programming in Fortran, and I am looking for a good ... reference on doing some input parsing tasks. ... What is a good reference for learning to parse strings in fortran? ... Loop through the header lines (read, goto). ...
    (comp.lang.fortran)
  • Re: how to use %val() ???
    ... >> It is an oft repeated falacy that Fortran arguments are always ... > conform to pass by reference semantics, ... > implementations even bother to try to conform to such ... > passing POINTERs everywhere. ...
    (comp.lang.fortran)
  • Re: On Call By Value
    ... call) by reference has been particularly surreal for someone who ... are used to some other languages, ... Fortran or with var parameters in Pascal, ... like Fortran and PL/I, in which the called routine is handed ...
    (comp.lang.c)
  • Re: Fortran and .NET (C#)
    ... >> I seem to remember Fortran 90 allowing arrays to be returned by ... No reference counts or garbage collection is needed. ... Is it true that memory that a Fortran pointer "points" to may not be ... > Recall that, unlike C, Fortran doesn't force you into using pointers just ...
    (comp.lang.fortran)