In procedures, DATA statements perform a role for which assignment statements are no substitute. It is quite often necessary to arrange for some action to be carried out at the start of the first call but not subsequently, such as opening a file or initialising a variable or array which accumulates information during subsequent calls.
If information is preserved in a local variable or array from one invocation to another a SAVE statement (described in section 9.11) is also needed. Indeed, in general any object initialised in a DATA statement in a procedure also needs to be named in a SAVE statement unless its value is never altered.
In the next example the procedure opens a data file on its first call, using a logical variable OPENED to remember the state of the file.
SUBROUTINE LOOKUP(INDEX, RECORD) INTEGER INDEX REAL RECORD LOGICAL OPENED SAVE OPENED DATA OPENED / .FALSE. / *On first call OPENED is false so open the file. IF(.NOT. OPENED) THEN OPEN(UNIT=57, FILE='HIDDEN.DAT', STATUS='OLD', $ ACCESS='DIRECT', RECL=100) OPENED = .TRUE. END IF READ(UNIT=57, REC=INDEX) RECORD END
Here, for simplicity, the I/O unit number is a literal constant. The procedure would be more modular if the unit number were also an argument of the procedure or if it contained some code, using the INQUIRE statement, to determine for itself a suitable unused unit number.
There is, of course, no corresponding way to determine which is the last call to the procedure so that the file can be closed, but this is not strictly necessary as the Fortran system closes all files automatically when the program exits.
Note that DATA statements cannot be used to initialise variables or arrays which are dummy arguments of a procedure, nor the variable which has the same name as the function.