Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site lanl.ARPA
Path: utzoo!watmath!clyde!burl!ulysses!allegra!bellcore!decvax!genrad!teddy!panda!talcott!harvard!seismo!cmcl2!lanl!jlg
From: jlg@lanl.ARPA
Newsgroups: net.lang
Subject: Re: Constants in subroutine calls
Message-ID: <19317@lanl.ARPA>
Date: Fri, 11-Jan-85 14:50:11 EST
Article-I.D.: lanl.19317
Posted: Fri Jan 11 14:50:11 1985
Date-Received: Sun, 13-Jan-85 08:18:25 EST
References: <468@ecsvax.UUCP> <606@spuxll.UUCP>
Sender: newsreader@lanl.ARPA
Organization: Los Alamos National Laboratory
Lines: 46

> This awful FORTRAN behavior is wired into the STANDARDS, to wit: FORTRAN uses
> call by 'value-result': each argument is copied, the copies are passed, and
> at return the copies are copied back to the original arguments.  This is not
> quite the same as call by reference, since (for example) you can pass a
> variable which is in COMMON and the subroutine can modify either copy without
> the effects being replicated in the other -- until it returns!

I just reread the standard, and that's NOT what it says. 'Value-result' is
not required or implied by the standard (a standard conforming compiler is
allowed to use 'value-result' if it remains semantically true to the
standard's requirements).  I've never worked with a Fortran compiler which
used 'value-result' parameter passing (I have used several non-Fortran
languages with this parameter passing method).

According to the standard, you CAN'T pass a variable in both common and as
a dummy argument (ANSI X3.9-1978 sec. 15.9.3.6).  I don't know of any way
for the compiler to check for violations of this constraint, but it is non-
standard.

> Unfortunately, many compiler writers simply implemented call by reference.
> You can test your compiler with the above example: pass a COMMON variable,
> modify the parameter, and see if the COMMON variable reflects the change
> WHILE STILL IN THE SUBROUTINE.  If so, you have call by reference.

Most Fortrans use call by reference for ALL parameters.  It's not
unfortunate, it's just the way things are.  I've never found it to be a
hardship.

> Note that either call by reference or call by value-result will clobber your
> constants.  The trick recently suggested -- pass (1.0) rather than 1.0 --
> is a nice way to defeat this in FORTRAN, works fine in PL/I (call by
> reference) also.

A good Fortran compiler will pass a copy of the constant, not the original
in read-only memory.  For example, The CRAY Fortran compiler makes a copy
of all constant arguments into the temporary data area and passes these
copies to the subroutine (the exception is that character constants are
passed directly (I think - will have to check)).

I think that the constraint against passing constants to subroutines which
change argument values (ANSI X3.9-1978 sec. 15.9.2) should be removed.  I
like to pass constants to such routines when I don't care what the returned
value is going to be, and the technique of passing copies allows this to
work just fine.

                                                James Giles