bob.sacamento
bob.sacamento

Reputation: 6651

When do FORTRAN subprograms save data, and when not?

Have a pretty simple function for taking the name of a month, "Jan", "Feb", etc. and converting to the number of the month:

function month_num(month_str)

  character*(*) :: month_str
  character*3 :: month_names(12)
  integer :: ipos(1),location(12)

  data month_names/'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug', &
                   'Sep','Oct','Nov','Dec'/

  where (month_names==month_str) location=1

  ipos = maxloc(location)
  month_num = ipos(1)

end function

And OK, yes, I know it's dangerous to not define "location" before using it.

Problem: During execution of the function, if input is OK, some value of "location" will be set to 1. And, to my surprise, when the function is called again, that value will still be equal to 1. And this, of course, really messes things up. So I figured I would fix it with a new line

data location/12*0/

And I got the same problem.

Finally, I put in

location = 0

just before the "where" statement, and that fixed everything.

So, I thought FORTRAN subprograms would not save data unless the variables were declared with the "SAVE" attribute. Also, with many compilers, you can invoke some sort of "static" option that will keep everything saved. I did neither of these here, but the "location" array was saved just the same. Can someone enlighten me on the rules of when FORTRAN saves data and when not? Thanks.

Upvotes: 2

Views: 136

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78316

The value of a variable local to a procedure is preserved across (ie it is SAVEd) in one of two ways:

  1. The programmer specifies the SAVE attribute when declaring the variable, for example:

    REAL, SAVE :: var1

  2. The programmer initialises the variable upon declaration, for example

    REAL :: var1 = 3.1415

This second, implicit, behaviour is one features of Fortran which seem designed to catch out the programmer, and not just beginners. Note that the value the variable has upon re-invocation is not, in the 2nd example 3.1415, but whatever value it had when the last invocation exited.

It is common for compilers to behave as if a variable is SAVEd when the programmer has not exercised either of these options, perhaps the memory locations used by one invocation of a procedure are not overwritten before the next invocation. But this behaviour is not to be relied on.

The situation is slightly different for variables declared in modules. Again any variable with the SAVE attribute is saved but any other variable only retains its value while the module is use-associated with a program unit which has started executing but not finished. Again some compilers, and some executions of some programs, may behave as if the value of a module variable is preserved despite the module going out of scope but this is non-standard behaviour and not to be relied on.

This behaviour is scheduled to change in Fortran 2008 when variables defined in modules will acquire the SAVE attribute implicitly.

Personally I like to explicitly SAVE variables even when I am sure that they would get the attribute implicitly, it makes the code just a bit easier to understand next time round.

Upvotes: 3

Related Questions