Lahey Support
08-15-2003, 01:24 AM
Hi all
My understanding of this was that (certainly in F90+) variables were local
to a scoping unit (includes sub-routine/function) unless declared otherwise.
Yes, if you change the value of an argument in a subroutine/function, that
value will be transferred from the dummy argument to the real argument on
return.
My limited imagination is obviously preventing me from seeing some obvious
application for doing a 'clayton's' change in the value of an argument.
If one is trying to prevent code from accidentally changing the value of an
input (actual) argument, then perhaps in F77 one uses what you have
suggested. In F90 the INTENT specification is available.
What am I missing here gals and guys ?
Gaz
----------
From: Omer akgiray [address removed]
Sent: Friday, 18 February 2000 16:32
To: Claudio Ona; [address removed]
Subject: Re: [LF] Forcing Passing by Value
Hello,
> 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.
>
OK, so I should have said something like "when a single variable
appearing
as an actual argument is enclosed in parentheses, memory address of
a copy
(i.e. result of the expression) is passed rather than the memory
address of
the original variable itself."
The point I was trying to make was: a variable appearing as an
actual
argument can be protected from modification in a procedure by using
enclosing parentheses.
Microsoft FORTRAN manual states "If an actual argument is a
constant, a
function reference, or an expression other than a single variable,
assigning
a value to the corresponding formal (dummy) argument is not
permitted and
has unpredictable results." Be that as it may, using enclosing
parentheses
does not lead to any error messages with this compiler and the
variable
appearing as the actual argument is successfully protected from
modification. But, from what you say I understand that this will not
work
with all compilers.
> 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.
If I were writing the procedure in question, I would make local
copies of
the dummy arguments in question and modify the values of the local
variables
(if, for some reason this is needed) to protect the corresponding
actual
arguments. (If using F90 one could specify the INTENT(IN) attribute
for the
mentioned dummy arguments.)
If this approach is not feasible for some reason, one could make
copies of
the variables in question in the calling routine and then use those
copies
as actual arguments. (This uses extra memory.) That their values are
modified by the procedure would then not matter and the original
variables
would not be modified. Perhaps I am not thinking very clearly at the
moment,
but the "enclosing parentheses approach" seemed much simpler and
neater.
Thanks for your response.
Omer Akgiray
>
> 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
-----------------------------------------------------
-----------------------------------------------------
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
-----------------------------------------------------
My understanding of this was that (certainly in F90+) variables were local
to a scoping unit (includes sub-routine/function) unless declared otherwise.
Yes, if you change the value of an argument in a subroutine/function, that
value will be transferred from the dummy argument to the real argument on
return.
My limited imagination is obviously preventing me from seeing some obvious
application for doing a 'clayton's' change in the value of an argument.
If one is trying to prevent code from accidentally changing the value of an
input (actual) argument, then perhaps in F77 one uses what you have
suggested. In F90 the INTENT specification is available.
What am I missing here gals and guys ?
Gaz
----------
From: Omer akgiray [address removed]
Sent: Friday, 18 February 2000 16:32
To: Claudio Ona; [address removed]
Subject: Re: [LF] Forcing Passing by Value
Hello,
> 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.
>
OK, so I should have said something like "when a single variable
appearing
as an actual argument is enclosed in parentheses, memory address of
a copy
(i.e. result of the expression) is passed rather than the memory
address of
the original variable itself."
The point I was trying to make was: a variable appearing as an
actual
argument can be protected from modification in a procedure by using
enclosing parentheses.
Microsoft FORTRAN manual states "If an actual argument is a
constant, a
function reference, or an expression other than a single variable,
assigning
a value to the corresponding formal (dummy) argument is not
permitted and
has unpredictable results." Be that as it may, using enclosing
parentheses
does not lead to any error messages with this compiler and the
variable
appearing as the actual argument is successfully protected from
modification. But, from what you say I understand that this will not
work
with all compilers.
> 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.
If I were writing the procedure in question, I would make local
copies of
the dummy arguments in question and modify the values of the local
variables
(if, for some reason this is needed) to protect the corresponding
actual
arguments. (If using F90 one could specify the INTENT(IN) attribute
for the
mentioned dummy arguments.)
If this approach is not feasible for some reason, one could make
copies of
the variables in question in the calling routine and then use those
copies
as actual arguments. (This uses extra memory.) That their values are
modified by the procedure would then not matter and the original
variables
would not be modified. Perhaps I am not thinking very clearly at the
moment,
but the "enclosing parentheses approach" seemed much simpler and
neater.
Thanks for your response.
Omer Akgiray
>
> 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
-----------------------------------------------------
-----------------------------------------------------
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
-----------------------------------------------------