Path: utzoo!attcan!uunet!maverick.ksu.ksu.edu!deimos.cis.ksu.edu!mccall!tp From: tp@mccall.com Newsgroups: comp.lang.fortran Subject: Summary: wrong Message-ID: <3250.26b00265@mccall.com> Date: 27 Jul 90 08:59:17 GMT References: <2009@key.COM> <58237@lanl.gov> Organization: The McCall Pattern Co., Manhattan, KS, USA Lines: 59 In article <58237@lanl.gov>, jlg@lanl.gov (Jim Giles) writes: > From article <2009@key.COM>, by sjc@key.COM (Steve Correll): >> [...] >> X .GT. Y .OR. L(Z) >> >> where L is a logical function which defines its dummy argument. The standard >> says that if X is greater than Y, then Z becomes undefined. Not very user- >> friendly, but Fortran programmers have traditionally been tough as nails. > > Since this started as a comparison to C, let's remember that C is just > as bad here. You can't predict a-priori whether the function L() will > be executed in either language. In pretty much any language, it's a > bad idea to have functions with side-effects in a context like this. Not true. In the expression "x > y || l(z)", l will only be called if x is not greater than y. The definition of the || (or) and && (and) operators explicitly state that evaluation is left to right, and the right hand side will only be evaluated if necessary to determine the value of the expression. For example, in C one can safely say if( a >= 0 && x[a] == 0)... i.e. check the range of a subscript and use it if it is valid, in the same conditional. The corresponding FORTRAN if(a.ge.1.and.x(a).eq.0)... is NOT safe, because the subscript may be used before its validity is checked, since FORTRAN does not specify the order of evaluation, nor guarantee that all parts won't be evaluated. (Note that C arrays have a lower bound of 0, whereas FORTRAN defaults to 1.) I'm not advocating functions with side-effects being used this way (it tends to hide the function call and make the program harder to understand), but it does work reliably in C. The above example is more signficant, because it is a more common need. The equivalent fortran requires at least 2 statements to perform the check. If the if is an if-then, it will also require a scratch logical variable or a nested if-then, i.e.: if(a.ge.1)flag = x(a).eq.0 if(flag)then ... endif or if(a.ge.1)then if(x(a).eq.0)then ... endif endif In this case the C code is much cleaner, since it does what the average user would expect, whereas it is not obvious to the novice user why the FORTRAN example won't work (this precise usage is a VERY common novice programming error). -- Terry Poot The McCall Pattern Company (uucp: ...!rutgers!ksuvax1!mccall!tp) 615 McCall Road (800)255-2762, in KS (913)776-4041 Manhattan, KS 66502, USA