The AUTOMATIC and STATIC statements are used within a called subprogram to control the allocation of storage to variables and the initial value of variables.
The AUTOMATIC and STATIC statements take the following form:
AUTOMATIC v [,v] . . . STATIC v [,v] . . .
The following table summarizes the differences between automatic and static variables upon entry to and exit from a subprogram.
Variable | Upon Entry | Upon Exit |
---|---|---|
Automatic | Variable is undefined. It does not reflect changes caused by any previous execution of the subprogram. | The storage area allocated to the variable is deleted. |
Static | Variable is defined with the value from the last execution of the subprogram.[1] | The current value of the variable is kept in the static storage area. |
[1] If there has been no previous execution, the variable is undefined. |
By default, all variables are static. To change the default from
static to automatic, specify the compiler option RECURSIVE or
AUTOMATIC.
To override the compiler option in effect for individual variables,
specify the variables in AUTOMATIC or STATIC statements.
Automatic variables can reduce memory use because only the variables
currently being used are allocated to memory.
Automatic variables permit recursion. With recursion, a
subprogram can call itself (directly or indirectly), and resulting
values are available upon a subsequent call or return to the
subprogram.
The following example shows a valid AUTOMATIC statement:
The following example shows a valid STATIC statement:
INTEGER I, J(10)
AUTOMATIC I, J, K(20)
REAL C, D, E(30)
STATIC C, D, E