Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!amdcad!ames!amelia!lemming.nas.nasa.gov!fouts From: fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) Newsgroups: comp.lang.fortran Subject: Fortran function side effects (was Re: i++, i+=1, i=i+1) Message-ID: <1000@amelia.nas.nasa.gov> Date: 16 Sep 88 16:33:54 GMT References: <3976@h.cc.purdue.edu> <3659@lanl.gov> Sender: news@amelia.nas.nasa.gov Reply-To: fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) Lines: 51 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. +-+-+-+