fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) (09/16/88)
Keywords: In article <3659@lanl.gov> jlg@lanl.gov (Jim Giles) writes: > >No, it doesn't cause the subscript to be evaluated twice. In Fortran >functions are not allowed to have side effects. I'm not sure what the standard says about function side effects, but I just checked a little program and several implementations allowed two kinds of side effects. Both modification of variables contained in common blocks and i/o were allowed by these compilers. Should I be reporting this as a compiler bug (and thereby breaking a lot of code which modifies common data in functions) or does Fortran actually allow side effects in functions? Here's the program I tried, which on several machines output I = 1 I = 1 J = 1 and on no machine generated a compiler error, or output I = 1 J = 0 Marty PROGRAM SIDETEST C C Can functions have side effects? C COMMON /SIDE/ K K = 0 I = J(1) WRITE(6,100) I, K 100 FORMAT(' I = ', I5, ' J = ', I5) END C FUNCTION J(I) COMMON /SIDE/ K WRITE (6,100) I 100 FORMAT(' I = ', I5) J = I K = I RETURN END +-+-+-+ I don't know who I am, why should you? +-+-+-+ | fouts@lemming.nas.nasa.gov | | ...!ames!orville!fouts | | Never attribute to malice what can be | +-+-+-+ explained by incompetence. +-+-+-+
jlg@lanl.gov (Jim Giles) (09/17/88)
From article <1000@amelia.nas.nasa.gov>, by fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts): > I'm not sure what the standard says about function side effects, but I > just checked a little program and several implementations allowed two > kinds of side effects. Both modification of variables contained in > common blocks and i/o were allowed by these compilers. Should I be > reporting this as a compiler bug (and thereby breaking a lot of code > which modifies common data in functions) or does Fortran actually > allow side effects in functions? Actually, most Fortran processors allow side effects in functions as an extension to the standard. Also, most Fortran processors optimize function calls in expressions as if they _didn't_ have side effects. Extensions to the standard are perfectly legal (see section 1.4 of the ANSI Fortran manual), so there's no cause for complaint on that score. Actually, _I_ think that any Fortran processor which allows side effects in functions should also have a SIDE EFFECTS declaration flag on the EXTERNAL declaration statement. This would have the same effect on function calls as the 'volatile' keyword in ANSI C has on variables. I agree that any Fortran which allows side effects without providing a safeguard against mistakes is not adequate. But, no one really ever seems to complain. In any case, the semantic argument about '+=' hinges on the use of side effect functions used in subscripts - something which is _very_ rare. (I've only done it once. It was a program which shuffled a deck of cards and I used the random number generator to index into the representation of the deck. But I didn't use that index on both sides of the same assignment statement, so it doesn't count in this discussion.) J. Giles Los Alamos