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



On Fri, 07 Oct 2005 19:38:42 GMT, "James Giles"
<jamesgiles@xxxxxxxxxxxxxxxx> wrote:

> 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

for parameters/formals/dummies declared as pointer, or as array (which
_for a parameter only_ in C is really a pointer)

> 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.
>
Right.

> 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.)
>
The C code will set the actual to and output 100.0 twice.
(Actually, if we consider %g as closest corresponding to
list or "free" format, it might be 100 twice. But that's
irrelevant to your and my points.)

> In spite of this being illegal, most Fortran
> implementations won't detect the error and will
> permit the call. The result *might* be <snip>

> 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

Although depending on the processor (caching) and perhaps OS this
might not actually be any slower. And in this example the difference
will be swamped by the cost of doing even one I/O, but of course in
real computational code that probably wouldn't be there.

> 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

C99 also added complex. Odd coincidences, those. :-)

> 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).
>
Although F9x POINTERs have in some cases more specific target types
than C pointers do, and thus may produce somewhat less false aliasing.

> 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.

Strongly agree.

- David.Thompson1 at worldnet.att.net
.



Relevant Pages

  • Re: Fortran and .NET (C#)
    ... Was there "garbage collection" in Fortran 90 or was there reference counting? ... For functions returning arrays that are either explicit-sized or allocatable, the language is designed such that you don't need either of those. ... And if one is a novice, well, one pretty much abandons all hope of a bug-free program if one uses functions that return pointers. ... Recall that, unlike C, Fortran doesn't force you into using pointers just because you have arrays. ...
    (comp.lang.fortran)
  • 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 ... whereas C implementations are *required* to ... conform to pass by reference semantics, ... call by reference semantics. ...
    (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)