HP 3000 Manuals

Using the SAVE Statement [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Using the SAVE Statement 

The SAVE statement retains the value of local variables after the
execution of a RETURN or END statement in a function or subroutine.  The
SAVE statement allows data to be shared among subprograms because the
values of entities are saved beyond the scope of the program units in
which they are declared.  However, an item in a common block can become
undefined or redefined in another unit.

The items that can be specified by the SAVE statement are:  named common
blocks enclosed in slashes, a variable name, or any array name.  Each
item can appear only once.  The items that cannot be specified by the
SAVE statement are:  dummy argument names, subprogram names, and names of
individual items in a common block.  If no individual items are
specified, all variables, arrays, and common block data are saved.

If a common block name is in a SAVE statement in one subprogram of an
executable program, the block name must be specified in a SAVE statement
in every subprogram in which it appears.  A common block name surrounded
by slashes in a SAVE statement specifies all the entities in the block.

Execution of a RETURN or END statement within a subprogram causes the
items in a subprogram to become undefined, except for:

   *   Items specified by SAVE statements.

   *   Items in common blocks that are not declared in the calling
       program.

   *   Items that have been defined in a DATA statement and not
       redefined.

The following program shows show the SAVE statement works:

           PROGRAM main
           INTEGER sub1

           WRITE(6,*) sub1(.TRUE.), sub1(.FALSE.),
          *           sub1(.FALSE.), sub1(.FALSE.)

           END

           INTEGER FUNCTION sub1(first)
           INTEGER count
           LOGICAL first
           SAVE

           IF(first) count = 0

           count = count + 1
           sub1 = count

           RETURN
           END

The output of this program looks like this:

     1 2 3 4

The variable count is incremented each time sub1 is called.  The SAVE
statement in the subroutine saves the value of count until the next call
to sub1.



MPE/iX 5.0 Documentation