PDA

View Full Version : RE: [LF] Forcing Passing by Value


Lahey Support
08-15-2003, 01:24 AM
Hi,

My understanding is that Fortran never passes the variables by values, as
you might do in other languages where the value is copied to the call stack.
By enclosing the variable name in parenthesis you are creating an expression
which is evaluated before the CALL is performed and it's result is stored
in memory.
Then memory address of the result is passed in the call stack.

F90 allows array expressions. Your F90 example is doing exactly the same
with an array expression as the F77 compiler did with a scalar expression.

Some compilers store the results of these expressions in protected memory
areas. So trying to write on them will give an "access violation" type of error.
I do not know what the standard says about it, but in general it is not a good
idea to write on expressions passed as subroutine arguments.

Regards,

+ --------------------------------+
| Claudio Ona |
| Studsvik Scandpower A/S |
| Phone: +54 (351) 425 3653 |
| e-mail: [address removed] |
+ --------------------------------+


On Thursday, February 17, 2000 11:42 AM, Omer akgiray [address removed] wrote:
> << File: ATT00002.txt; charset = windows-1254 >> << File: ATT00003.htm; charset = windows-1254 >>
>
> Hello,
>
> While FORTRAN normally passes variables by reference, one can also force
> passing by value in standard FORTRAN 77. This is accomplished by enclosing
> the actual argument in parentheses.
>
> In the following program, for example,
>
> REAL A, B
> A=20.
> B=20.
> DUMMY=TEST(A)
> DUMMY=TEST((B))
> PRINT*, A, B
> END
>
> FUNCTION TEST(X)
> X=1.0
> TEST=0.
> END
>
> the output becomes:
>
> 1.0 20.0
>
> Thus, B is protected from modification.
>
> My understanding is (was) that you could do this with simple variables
> (like A and B) and array elements, but not with array names.
> I distinctly remember that a FORTRAN 77 compiler I used to use many years
> ago did not allow placing enclosing parantheses around array names appearing
> as actual arguments.
>
> For example,
>
> REAL Z(2)
> Z(1)=100.
> Z(2)=100.
> DUMMY=TEST2((Z))
> PRINT*, Z
> END
>
> FUNCTION TEST2(X)
> REAL X(2)
> X(1)=-5.
> X(2)=-5.
> TEST2=0.
> END
>
> would generate an error message (during compilation) complaining about
> Z appearing in variable context. But this program compiles and run
> s without errors using MS PowerStation (Fortran 90) compiler, with the output
>
> 100.0 100.0
>
> My question is the following: Is this (i.e. ability to force passing
> an actual argument array by value by enclosing it in parantheses) a new feature
> in Fortran 90 or is this an extension implemented in MS Fortran?
>
> Thanks in advance.
>
> Omer Akgiray
> Dept. of Environmental Engg.
> Marmara University, Istanbul
>


-----------------------------------------------------
To unsubscribe from the Fortran Forum, send a message
to [address removed] with the following command as
the first and only line of the message body:
unsubscribe fortran
-----------------------------------------------------