[comp.lang.fortran] Question about variables local to subroutine

templon@silver.bacs.indiana.edu (jeffrey templon) (08/23/89)

I would like to know one simple (I hope) thing: what happens if you set
the value of a variable (local) in a subroutine, exit the subroutine, and
come back later?  Is it the same value, zero, or worse?  I read all TFMs
I could find and saw no answer to this one.  I am using VAX Fortran,
whatever version runs on VMS V4.6.  Thanks for any help you can send.

					Jeff Templon

ghe@nucthy.physics.orst.edu (Guangliang He) (08/23/89)

In article <24891@iuvax.cs.indiana.edu> templon@silver.bacs.indiana.edu (jeffrey templon) writes:
=
=I would like to know one simple (I hope) thing: what happens if you set
=the value of a variable (local) in a subroutine, exit the subroutine, and
=come back later?  Is it the same value, zero, or worse?  I read all TFMs
=I could find and saw no answer to this one.  I am using VAX Fortran,
=whatever version runs on VMS V4.6.  Thanks for any help you can send.
=
=					Jeff Templon


As my understanding, Fortran 77 standard does NOT save the value of a local
variable unless you use save in the subroutine. But I am not very sure about
the detail. A lot of compilers do save the value of local variables. The
Unix f77 does it. But I have heard the oppesite case. The Mac Fortran
doesn't save them. You should read the manual carefully to see how VAX
Fortran does these kind things.
-----------------------------------------------------------------------
                                   |
USMAIL:   Guangliang He            |  INTERNET: ghe@PHYSICS.ORST.EDU
          Department of Physics    |  BITNET:   hegl@ORSTVM.BITNET
          Oregon State University  |
          Corvallis, OR 97331      |  PHONE:    (503) 737-4631
                                   |
-----------------------------------------------------------------------

khb@road.Sun.COM (Keith Bierman - Advanced Languages - Floating Point Group ) (08/23/89)

In article <24891@iuvax.cs.indiana.edu> templon@silver.bacs.indiana.edu (jeffrey templon) writes:
>
>I would like to know one simple (I hope) thing: what happens if you set
>the value of a variable (local) in a subroutine, exit the subroutine, and
>come back later?  Is it the same value, zero, or worse?  I read all TFMs
>I could find and saw no answer to this one.  I am using VAX Fortran,
>whatever version runs on VMS V4.6.  Thanks for any help you can send.
>
>					Jeff Templon


Well, if you read the standards document (:> turgid prose at best),
you will find that if you declare your variable

	subroutine sub(stuff)

	real your_var  ! extended fortran, for long var name and underscore
	save your_var  ! not to mention lower case and !

the standard assures you that your_var has the result from the last
invocation. 

If you don't employ save, vendor gets to do whatever they want.

DEC VMS FORTRAN happens to make all variables SAVEd for you anyway.

UNIVAC (used to) gives you junk. Sun (well me) strongly advises use of
the SAVE, if you really want your old value back.

I know of no one who promises you zero ... many machines (dec, sun)
give you zero the first time through ... second time zero is about the
least likely number.

-- 
Keith H. Bierman    |*My thoughts are my own. !! kbierman@sun.com
It's Not My Fault   |	MTS --Only my work belongs to Sun* 
I Voted for Bill &  | Advanced Languages/Floating Point Group            
Opus                | "When the going gets Weird .. the Weird turn PRO"
Keith H. Bierman    |*My thoughts are my own. !! kbierman@sun.com
It's Not My Fault   |	MTS --Only my work belongs to Sun* 
I Voted for Bill &  | Advanced Languages/Floating Point Group            
Opus                | "When the going gets Weird .. the Weird turn PRO"

jkw@alpha.lanl.gov (Jay Wooten) (08/24/89)

In article <24891@iuvax.cs.indiana.edu>, templon@silver.bacs.indiana.edu (jeffrey templon) writes:
> 
> I would like to know one simple (I hope) thing: what happens if you set
> the value of a variable (local) in a subroutine, exit the subroutine, and
> come back later?  Is it the same value, zero, or worse?  I read all TFMs
> I could find and saw no answer to this one.  I am using VAX Fortran,
> I could find and saw no answer to this one.  I am using VAX Fortran,
> whatever version runs on VMS V4.6.  Thanks for any help you can send.
> 
> 					Jeff Templon

 Look in  the "Programming in VAX FORTRAN" manual under the description of the
 SAVE statement (pp. 8-24 and 8-25 in the 4.0 version):

 "The SAVE statement causes the definition of data entities to be retained
  after execution of a RETURN or END statement in a subprogram."
 ...
 "NOTE: It is not necessary to use SAVE statements in VAX FORTRAN programs.
  The definitions of data entities are retained automatically by VAX FORTRAN,
  making the use of SAVE statements a redundant exercise [unless you want to
  port your program to a non-VMS system]."

  In my experience, variables in a VMS FORTRAN subprogram are always the same
  values as you left them the last time.


	       ~~~~~~ Inquiring minds want to know ! ~~~~~~
	  Jay Wooten  Los Alamos National Lab  ARPA: jkw@lanl.gov

urjlew@ecsvax.UUCP (Rostyk Lewyckyj) (08/24/89)

In a similar vein to saving values of local variables in subroutines
across invocations. What does the standard and experience say
about saving of array shape parameters passed in through an ENTRY
statement
    Subroutine xyz
    dimension  a(m,n)
     ......
     x=a(i,j)
     .....
     return
     ENTRY setup(a,m,n)
     return
     end
Setup is called once from some point in the program.
xyz is called many times from various points in the program,
not  necessarily from the same routine that calls setup.
THis works on IBM 370 type systems, MVS, VM with VS Fortran
compiler, at least in programs where xyz is not overlaid/reloaded.
This is/was a way of improving performance by eliminating the
need to pass a,m,n through all the paths needed to include it
in the xyz call, and also avoiding the additional complexity of
getting it done correctly. 
-----------------------------------------------
  Reply-To:  Rostyslaw Jarema Lewyckyj
             urjlew@ecsvax.UUCP ,  urjlew@unc.bitnet
       or    urjlew@uncvm1.acs.unc.edu    (ARPA,SURA,NSF etc. internet)
       tel.  (919)-962-9107

mike@hpfcdc.HP.COM (Mike McNelly) (08/24/89)

If the compiler supports recursion (not in standard FORTRAN 77), it is
unlikely that you can count on implicit value saving from one subprogram
invocation to the next.  Many (most?)  implementations that support
recursion use a separate stack frame for each function activation.
Sometimes these stack frames align from one invocation to the next but
there's certainly no guarantee that this will always be the case.

If you need to save values use the standard SAVE statement.  It will
save you considerable grief should you ever port your program to a new
architecture.

Mike McNelly
mike%hpfcla@hplabs.hp.com