Path: utzoo!attcan!uunet!pyrdc!pyrnj!rutgers!cmcl2!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: comp.lang.fortran Subject: Re: i++, i+=1, i=i+1 Message-ID: <3659@lanl.gov> Date: 16 Sep 88 02:24:55 GMT References: <3976@h.cc.purdue.edu> Organization: Los Alamos National Laboratory Lines: 23 From article <3976@h.cc.purdue.edu>, by ags@h.cc.purdue.edu (Dave Seaman): > If you apply your preprocessor to > x[i*f(j)+k*g(l)] += y > you get > x[i*f(j)+k*g(l)] = x[i*f(j)+k*g(l)] + y > which is not the same expression at all, becauses it causes the the subscript > to be evaluated twice. No, it doesn't cause the subscript to be evaluated twice. In Fortran functions are not allowed to have side effects. The compiler will therefore recognize the subscript expressions as common subexpressions and will evaluate only once. One guy had rand() in his example of subscript expressions - ever wonder why rand() isn't part of the Fortran standard? Ever wonder why most Fortran compilers warn against using rand() more than once in the same expression? This is why. Actually an expression like RAND(1)+RAND(2) _will_ call the random generator twice. Fortran assumes the lack of side effects, but a function applied to different arguments must be repeated for each different argument. Ever wonder why rand() takes an argument which it ignores? J. Giles Los Alamos