Values retained by function between calls? 
Author Message
 Values retained by function between calls?

Does a f90 program remember the values of variables in a function or
subroutine between calls?

Sorry to ask such an elementary question, but I seem to be getting
inconsistent results. With one function the values seem to be
remembered, but with another they aren't.
--
John Uebersax PhD



Wed, 24 Dec 2008 14:13:58 GMT  
 Values retained by function between calls?
Quote:

> Does a f90 program remember the values of variables in a function or
> subroutine between calls?

> Sorry to ask such an elementary question, but I seem to be getting
> inconsistent results. With one function the values seem to be
> remembered, but with another they aren't.
> --
> John Uebersax PhD

See
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread...

if you don't find the answer, you should describe the problem more
closely.
Jaroslav



Wed, 24 Dec 2008 14:28:09 GMT  
 Values retained by function between calls?

Quote:

> Does a f90 program remember the values of variables in a function or
> subroutine between calls?

This is known as the SAVE attribute. In general, no, although there are
cases where it happens. The most obvious case is if you explicitly
declare the SAVE attribute, but there are also others.

Since you haven't given any details, I can't comment usefully on the
difference between your two functions. I'm not about to try to elaborate
every possibly relevant word of the standard (that would be a lot of
them).

It is also fairly common for the values to happen to be retained by some
particular compilers in cases where the standard does not specify it.
Programs that depend on such behavior are non-standard.... but are also
pretty common, particularly in older codes.

If you want the values to be retained, specify the SAVE attribute and be
sure. That's what it is for.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Wed, 24 Dec 2008 15:08:45 GMT  
 Values retained by function between calls?

Thanks for your helpful reply.

Quote:
> Since you haven't given any details, I can't comment usefully on the
> difference between your two functions. I'm not about to try to elaborate
> every possibly relevant word of the standard (that would be a lot of
> them).

Sure.  That's not what I wanted anyway.  I'm not a newbie.  If I wanted
advice on particular code, trust me to supply sufficient details.

Quote:
> If you want the values to be retained, specify the SAVE attribute and be
> sure. That's what it is for.

That's what I need to know.  Thanks again.

--
John Uebersax PhD



Wed, 24 Dec 2008 19:25:56 GMT  
 Values retained by function between calls?

Quote:


> > Does a f90 program remember the values of variables in a function or
> > subroutine between calls?

> This is known as the SAVE attribute. In general, no, although there are
> cases where it happens. The most obvious case is if you explicitly
> declare the SAVE attribute, but there are also others.

> Since you haven't given any details, I can't comment usefully on the
> difference between your two functions. I'm not about to try to elaborate
> every possibly relevant word of the standard (that would be a lot of
> them).

> It is also fairly common for the values to happen to be retained by some
> particular compilers in cases where the standard does not specify it.
> Programs that depend on such behavior are non-standard.... but are also
> pretty common, particularly in older codes.

> If you want the values to be retained, specify the SAVE attribute and be
> sure. That's what it is for.

> --
> Richard Maine                    | Good judgement comes from experience;
> email: last name at domain . net | experience comes from bad judgement.
> domain: summertriangle           |  -- Mark Twain

I would add that, even if you are using a compiler that happens to
retain values, still specify the SAVE attribute (or if using a F77
compiler, specify the variable in a SAVE statement). This has several
advantages:

- No problems if porting to a different compiler/machine

- No problems if the retention of values was, in fact, unreliable

- It tells maintenance programmers that you intend to retain the value

Note that the third advantage makes it wise only to SAVE when necessary

Dave Flower



Wed, 24 Dec 2008 21:18:49 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. value dependent boolean to retain its sense when values would change boolean

2. VC++ calling fortran function and fortran function calling a c++ function

3. EIP column not retaining value

4. Check Box Properties in Dictionary does not retain True/False value

5. returning values through functions calls

6. Silos: Integers in functions values persist between calls.

7. Huh? func_defaults, default values in function calls

8. Call Array valued Fortran function from C

9. How update variable values by calling secondary functions ?

10. How to find out name of calling function from called function

11. Calling functions from functions from functions ...

12. Scheme values and call-with-values

 

 
Powered by phpBB® Forum Software