2

I came across some code today that looked somewhat like this:

      subroutine foo()
      real blah
      integer bar,k,i,j,ll
      integer :: n_called=1
      save integer

      ...
      end

It seems like the intent here was probably save n_called, but is that even a valid statment to save all integers -- or is it implicitly declaring a variable named integer and saving it?

mgilson
  • 300,191
  • 65
  • 633
  • 696

2 Answers2

5

The second interpretation is correct. Fortran has many keywords, INTEGER being one of them, but it has no reserved words, which means that keywords can be used as identifiers, though this is usually a terrible idea (but nevertheless it carries on to C# where one can prefix a keyword with @ and use it as an identifier, right?)

The SAVE statement, even if it was intended for n_called is superficial. Fortran automatically saves all variables that have initialisers and that's why the code probably works as intended.

 integer :: n_called=1

Here n_called is automatically SAVE. This usually comes as a really bad surprise to C/C++ programmers forced to maintain/extend/create new Fortran code :)

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Good point about `n_called` acquiring the save attribute implicitly. As for your last point, well, when you hire a boy for a man's job ... – High Performance Mark May 30 '12 at 14:40
  • Yeah, I figured the part about the implicit save as well -- Is that actually guaranteed by the standard, or is it just a *very* common implementation detail? -- Thinking about how in F77, `common` blocks weren't even guaranteed to be saved, it wouldn't surprise me if this was just a common thing that vendors do ... – mgilson May 30 '12 at 15:02
  • 1
    @mgilson: I believe that the SAVE status of a variable which is initialised on declaration is processor-dependent in FORTRAN 77, but that the language standards since, and including, Fortran 90 have guaranteed this behaviour. – High Performance Mark May 30 '12 at 16:57
  • AFAIK standard FORTRAN 77 doesn't allow variable initialisation the way it is done in Fortran 90. One has to use the `DATA` statement to assign initial values and it is processor- (a.k.a. compiler-) dependent behaviour if the variable is automatically saved or not (i.e. if `DATA` results in automatic or static storage class). One should use `SAVE` to enforce static storage. – Hristo Iliev May 30 '12 at 17:59
  • FORTRAN 77 vs Fortran 90 discussed at http://stackoverflow.com/questions/3352741/fortran-assignment-on-declaration-and-save-attribute-gotcha – M. S. B. May 30 '12 at 18:08
3

I agree with your 2nd interpretation, that is, the statement save integer implicitly declares a variable called integer and gives it the save attribute. Fortran, of course, has no rule against using keywords as program entity names, though most sensible software developers do have such a rule.

If I try to compile your code snippet as you have presented it, my compiler (Intel Fortran) makes no complaint. If I insert implicit none at the right place it reports the error

This name does not have a type, and must have an explicit type.   [INTEGER]

The other interpretation, that it gives the save attribute to all integer variables, seems at odds with the language standards and it's not a variation that I've ever come across.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • Yeah, that's what I thought too -- I should have thought about that silly `implicit none` test myself ... I guess I'm having an off day. Thanks. – mgilson May 30 '12 at 14:32