(j3.2006) Restrictions on side-effects for (non pure) functions?
Wed Apr 15 12:06:53 EDT 2015
On Apr 15, 2015, at 9:51 AM, Tom Clune <Thomas.L.Clune at NASA.GOV> wrote:
> I client sent an example that appears to produce incorrect answers under gfortran, but the expected results on other compilers. (Intel, NAG). A function with a side effect is used to return an index for an array reference. This works if the function is evaluated once, but it appears to be evaluated twice with gfortran.
I don?t see any semantics that would justify evaluating the function more than once before the WRITE statement.
> Before we file a bug report, I?m concerned that the abuse of side-effects is violating some prohibition in the standard. I?ve found several vague but ominous warnings on various websites, but have not found any relevant language in the latest standard.
The usual restrictions that apply are in 7.14 p2:
> the evaluation of a function reference shall neither affect nor be affected by the evaluation of any other entity within the statement;
> if a function reference causes definition or undefinition of an actual argument of the function, that argument or any associated entities shall not appear elsewhere in the same statement.
> Aside from the dubious programming practice - is the following a standard conforming code?
Looks OK to me.
> MODULE belt_and_suspenders
> INTEGER FUNCTION next(cnt)
> INTEGER, INTENT(INOUT) :: cnt
> cnt = cnt + 1
> next = cnt
> END FUNCTION next
> END MODULE belt_and_suspenders
> PROGRAM tryit
> USE belt_and_suspenders
> IMPLICIT NONE
> CHARACTER(LEN=64), DIMENSION(2) :: c
> INTEGER :: cntc
> cntc = 0
> c(next(cntc)) = 'a'
> WRITE(6,*) cntc
> END PROGRAM tryit
> J3 mailing list
> J3 at mailman.j3-fortran.org
Bill Long longb at cray.com
Fortran Technical Suport & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./ Cray Plaza, Suite 210/ 380 Jackson St./ St. Paul, MN 55101
More information about the J3