Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!rochester!PT.CS.CMU.EDU!sei!sei.cmu.edu!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.fortran Subject: Re: Semantics of local variables Message-ID: <3669@aw.sei.cmu.edu> Date: 4 Jan 88 14:12:53 GMT References: <2617@killer.UUCP> Sender: netnews@sei.cmu.edu Reply-To: firth@bd.sei.cmu.edu.UUCP (PUT YOUR NAME HERE) Organization: Carnegie-Mellon University, SEI, Pgh, Pa Lines: 33 Keywords: semantics local variables DATA In article <2617@killer.UUCP> richardh@killer.UUCP (Richard Hargrove) writes: >A quick question for serious FORTRAN user's: do the semantics of initializing >a local variable in a DATA statement force the variable to have static >scope? That is, can you generally count on the variable retaining its value >across calls to the subroutine in which it's defined? I know this is the case >for some compilers, but can you count on it generally? F77 and no, I don't >have a copy of the standard. The quick answer: No. You'll find it in ANSI X3.9-1978 para 17.3 (6) "The execution of a RETURN statement or an END statement within a subprogram causes all entities within the subprogram to become undefined except for the following: ... (b) Initially defined entities that have neither been redefined nor become undefined ... " In other words, if you haven't yet assigned to the variable, it will retain its initial value (as given in the DATA statement); if you have assigned to it (or caused it to become undefined in any of the many ways allowed by Fortran), then it is NOT guaranteed to retain any value between invocations of the subprogram. As you say, most compilers will let you get away with this, since they make the variables static. However, there are other legitimate implementations. If you want to follow the standard, don't ever assume more than it guarantees.