Copyright (c) 1994 J. Adams, W. Brainerd, J. Martin, and B. Smith.
All rights reserved.
This file may not be copied without permission of the authors.

82 SAVE Attribute and Statement

A variable with the SAVE attribute retains its value and definition, association, and allocation status on exit from a procedure. All variables accessible to a main program are saved implicitly. An entire common block may be saved in order to maintain the integrity of the storage when none of the procedures using the common block are active. Similarly, saving a variable in a module preserves its value when no procedure using the module is active.

MODULE FLOWERS
REAL, SAVE, ALLOCATABLE :: FOLIAGE(:) ! FOLIAGE is real type and
   . . .                              !   has the SAVE attribute.
END MODULE FLOWERS

SAVE A, B, TEMP, /BLOCKXY/            ! A common block, BLOCKXY
                                      !   has the SAVE attribute.

RECURSIVE SUBROUTINE ATLATL (X, Y)
   INTEGER :: COUNT = 0               ! COUNT is saved
      . . .                           !   automatically.
   COUNT = COUNT + 1
      . . .
   CALL ATLATL (X, Y)
      . . .
END SUBROUTINE ATLATL

SUBROUTINE DAISY
   SAVE                               ! This saves everything.
      . . .
END SUBROUTINE DAISY

Tip: Even though many implementations of Fortran 77 saved all variables and named common blocks, a standard-conforming program may not rely on this. Modern systems are more complex and more attention should be paid to variables that must retain their value in Fortran 90. Unless the SAVE attribute has been declared, a variable might not be saved. For the sake of portability, the SAVE attribute should always be declared for variables that need to retain their value.

Related Topics:

Data Initialization
Recursion

To Read More About It:

ISO 1539 : 1991, Fortran Standard, 5.1.2.5, 5.2.4
Fortran 90 Handbook, 5.5.1, 5.6.4, 12.1.3
Programmer's Guide to Fortran 90, 3.5, 10.2.2


A type declaration statement with the SAVE attribute is:

type , SAVE [ , attribute-list ] :: entity-list

A SAVE statement is:

SAVE [ [ :: ] saved-entity-list ]

A saved entity is one of:

data-object-name
/ common-block-name /

Things To Know:

  1. If the list in a SAVE statement is omitted in a scoping unit, everything in that scoping unit that can be saved is saved. No other explicit occurrences of the SAVE attribute or SAVE statement are allowed.
  2. A variable in a common block must not be saved individually. If a common block is saved in one program unit, it must be saved everywhere it appears other than in a main program.
  3. A SAVE statement in a main program has no effect because all variables and common blocks are saved implicitly in a main program.
  4. There is only one copy of saved variables in all activations in a recursive procedure. If a local variable is not saved, there is a different copy for each activation.
  5. Initialization in a DATA statement or in a type declaration implies that a variable has the SAVE attribute, unless the variable is in a named common block in a block data subprogram.
  6. The SAVE attribute may be declared in the specification part of a module. A variable in a module that is not saved becomes undefined when