# 5.17 SAVE Attribute and Statement

The SAVE attribute causes the values and definition of objects to be retained after execution of a RETURN or END statement in a subprogram.

The SAVE attribute can be specified in a type declaration statement or a SAVE statement, and takes one of the following forms:

Type Declaration Statement:

  type, [att-ls,] SAVE [, att-ls] :: [object [, object]...]

Statement:

  SAVE [object [, object]...]
  type
  Is a data type specifier.
  att-ls
  Is an optional list of attribute specifiers.
  object
  Is the name of an object, or the name of a common block enclosed in slashes (/common-block-name/).

Rules and Behavior

In Compaq Fortran, certain variables are given the SAVE attribute, or not, by default:

- The following variables are saved by default:
  - COMMON variables
  - Local variables of recursive subprograms
  - Data initialized by DATA statements
- The following variables are not saved by default:
  - Variables that are declared AUTOMATIC
  - Local variables that are allocatable arrays
  - Derived-type variables that are data initialized by default initialization of any of their components
  - RECORD variables that are data initialized by default initialization specified in its STRUCTURE declaration
- Local variables that are not described in the preceding two lists are saved by default.

To enhance portability and avoid possible compiler warning messages, Compaq recommends that you use the SAVE statement to name variables whose values you want to preserve between subprogram invocations.

When a SAVE statement does not explicitly contain a list, all allowable items in the scoping unit are saved.

A SAVE statement cannot specify the following (their values cannot be saved):

- A blank common
- An object in a common block
- A procedure
- A dummy argument
- A function result
- An automatic object
- A PARAMETER (named) constant

Even though a common block can be included in a SAVE statement, individual variables within the common block can become undefined (or redefined) in another scoping unit.

If a common block is saved in any scoping unit of a program (other than the main program), it must be saved in every scoping unit in which the common block appears.

A SAVE statement has no effect in a main program.

Examples

The following example shows a type declaration statement specifying the SAVE attribute:

    SUBROUTINE TEST()
      REAL, SAVE :: X, Y

The following is an example of the SAVE statement:

    SAVE A, /BLOCK_B/, C, /BLOCK_D/, E

For More Information:

- On type declaration statements, see Section 5.1.
- On common blocks, see Section 5.4.
- On the DATA statement, see Section 5.5.
- On recursive program units, see Section 8.5.1.1.
- On compatible attributes, see Table 5-1.
- On modules, see Section 8.3.

Previous Page Next Page Table of Contents