From: Thomas Koenig on
The following code definitely is ill-advised (a function changing
its arguments is not a good idea). Is it also illegal?

module foo
implicit none
contains
integer function f(i)
integer, intent(inout) :: i
i = i + 1
f = i
end function f
end module foo

program main
use foo
implicit none
integer :: i
i = 3
print *,f(i), i
end program main
From: Richard Maine on
Thomas Koenig <tkoenig(a)netcologne.de> wrote:

> The following code definitely is ill-advised (a function changing
> its arguments is not a good idea). Is it also illegal?
>
> module foo
> implicit none
> contains
> integer function f(i)
> integer, intent(inout) :: i
> i = i + 1
> f = i
> end function f
> end module foo
>
> program main
> use foo
> implicit none
> integer :: i
> i = 3
> print *,f(i), i
> end program main

Yes, it is illegal. Function side effects are the subject of a lot of
controversy. Some of it gets pretty subtle. Well-informed people
disagree about some points. But this case is about as easy as it gets,
the standard being pretty explicit on it and there not being much
question but that this case exactly fits the restriction in the
standard.

From f2003, 7.1.8 (The hardest part of this is finding where the
material is buried; I have a leg up in just remembering at least more or
less where it is; I didn't find it by any logical process.)

"The evaluation of a function reference shall neither affect nor be
affected by the evaluation of any other entity within the statement."

I'd think that would cover it, but that particular restriction is the
subject of some pretty subtle disagrements. The next sentence, however,
is

"If a function reference causes definition or undefinition of an actual
argument of the function, that actual argument or any associated
entities shall not appear elsewhere in the same statement."

Seems to me that one nails it; I don't see how you can interpret that
away.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: glen herrmannsfeldt on
Richard Maine <nospam(a)see.signature> wrote:
> Thomas Koenig <tkoenig(a)netcologne.de> wrote:

>> The following code definitely is ill-advised (a function changing
>> its arguments is not a good idea). Is it also illegal?

>> module foo
>> implicit none
>> contains
>> integer function f(i)
>> integer, intent(inout) :: i
>> i = i + 1
>> f = i
>> end function f
>> end module foo

>> program main
>> use foo
>> implicit none
>> integer :: i
>> i = 3
>> print *,f(i), i
>> end program main

> Yes, it is illegal. Function side effects are the subject of a lot of
> controversy. Some of it gets pretty subtle. Well-informed people
> disagree about some points. But this case is about as easy as it gets,
> the standard being pretty explicit on it and there not being much
> question but that this case exactly fits the restriction in the
> standard.

Just to be sure, it isn't changing function arguments that is
illegal, but using the changed argument in the same statement.

For example:

print *,f(i)
print *,i

Would be just fine.

The words of the OP only mentioned changing function arguments,
not how they were to be used.

It might be that the example to show the change unintentionally
used it in the same statement.

-- glen
From: Richard Maine on
glen herrmannsfeldt <gah(a)ugcs.caltech.edu> wrote:

> Richard Maine <nospam(a)see.signature> wrote:

> > Yes, it is illegal. Function side effects are the subject of a lot of
> > controversy... But this case is about as easy as it gets,
> > the standard being pretty explicit on it and there not being much
> > question but that this case exactly fits the restriction in the
> > standard.
>
> Just to be sure, it isn't changing function arguments that is
> illegal, but using the changed argument in the same statement.

Yes, that's why I quoted (in what you elided) the part of the standard
that says "shall not appear elsewhere in the same statement," which is
exactly what made this particular case pretty air tight.

> For example:
>
> print *,f(i)
> print *,i
>
> Would be just fine.

That is arguable. The arguments have been made here before. I don't feel
particularly inclined to rehash them. That's why I covered exactly the
case the OP asked about (and I find it hard to imagine that his using
the argument in the same statement was accidental; sure looked as
deliberate as I could imagine.) No, the debates have not been settled.
No, rehashing them would not likely change that.

Saying that the example above "would be just fine" appears to forget
about all the debates on the subject. I'll assume that to be the case
rather than any of the other, less generous, interpretations I can think
of.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain