Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!iuvax!pur-ee!uiucdcs!uxc.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald From: mcdonald@uxe.cso.uiuc.edu Newsgroups: comp.lang.fortran Subject: Re: Value parameters Message-ID: <50500012@uxe.cso.uiuc.edu> Date: Sun, 18-Oct-87 11:25:00 EDT Article-I.D.: uxe.50500012 Posted: Sun Oct 18 11:25:00 1987 Date-Received: Tue, 20-Oct-87 02:14:29 EDT References: <1331@cartan.Berkeley.EDU> Lines: 53 Nf-ID: #R:cartan.Berkeley.EDU:1331:uxe.cso.uiuc.edu:50500012:000:2562 Nf-From: uxe.cso.uiuc.edu!mcdonald Oct 18 10:25:00 1987 >/* Written 2:04 pm Oct 16, 1987 by naparst@cartan.Berkeley.EDU in uxe.cso.uiuc.edu:comp.lang.fortran */ >/* ---------- "Value parameters" ---------- */ >According to the fortran standard, value parameters must not be modified. >Why does the following program, then, give the output x=1, when it should >bomb in sub() ? This is f77 on 4.3. > > x = 1 > call sub(x + 7) > print *,x > stop > end > > subroutine sub(x) > x = 2 > return > end > >Harold Naparst (naparst@cartan.berkeley.edu or ucbvax!cartan!naparst) >-- >Harold Naparst (415)-848-4560 > UUCP {tektronix,dual,sun,ihnp4,decvax}!ucbvax!cartan!naparst > New style naparst@cartan.berkeley.edu > ARPA | CSNET naparst%cartan@berkeley.ARPA >/* End of text from uxe.cso.uiuc.edu:comp.lang.fortran */ Although Kurt Hirschert, U of I's resident F8X committee member give the correct (and definitive) answer to this whole discussion, I'm going to try one more time to explain it. The Fortran standards, both present and yet unborn, define in their own inimitable jargon what result a compliant compiler MUST produce for a compliant program. They do NOT define what a compiler must do for ALL programs which do not fit the standards. They do define certain instances of erroneous programs which a compiler must flag, but in certain cases compliant compilers are allowed to pass through things which are not completely defined (Kurt, is this true?). In those cases where the standard is mute, THE EXACT COMPILER YOU USE DEFINES THE "correct" RESULT. The standard says "If you, the program writer, pass an expression to a subroutine, I (ANSI) expect YOU not to modify that in the subroutine. If you *do* modify it, the compiler may tell you something or may not, in any case it (the compiler) DEFINES the action taken." Your example main program is a perfectly good Fortran program. The subroutine is a perfectly good subroutine. HOWEVER, according to the standards, THAT main program calling THAT subroutine is not 100% compliant (i.e. defined by the standard to to a particular thing). (Actually I'm not so sure about the print statement, but that's beside the point.) Fortran is VERY loose in forcing compilers (and linkers!) to examine subroutine linkage. It put responsibility on the programmer. Among other things, that allows wide latitude in doing tricky and useful things. Just be thankful that noone, to date, has written a compiler that detects such things and decides to dump you into Rogue! Doug McDonald