next up previous contents index
Next: Defined and Undefined Values Up: Professional Programmer's Guide to Previous: REWIND and BACKSPACE Statements

DATA Statement

 

The DATA statement is used to specify initial values for variables and array elements. The DATA statement is non-executable, but in a main program unit it has the same effect as a set of assignment statements at the very beginning of the program. Thus in a main program a DATA statement like this:

 
       DATA LINES/625/, FREQ/50.0/, NAME/'PAL'/

could replace several assignment statements:

 
       LINES = 625 
       FREQ  = 50.0 
       NAME  = 'PAL'

This is more convenient, especially when initialising arrays, and efficient, since the work is done when the program is loaded.

In a procedure, however, these two methods are not equivalent, especially in the case of items which are modified as the procedure executes. A DATA statement only sets the values once at the start of execution, whereas assignment statements will do so every time the procedure is called.

It is important to distinguish between the DATA and PARAMETER statements. The DATA statement merely specifies an initial value for a variable (or array) which may be altered during the course of execution. The PARAMETER statement specifies values for constants which cannot be changed without recompiling the program. If, however, you need an array of constants, for which there is no direct support in Fortran, you should use an ordinary array with a DATA statement to initialise its elements (and take care not to corrupt the contents afterwards).



 
next up previous contents index
Next: Defined and Undefined Values Up: Professional Programmer's Guide to Previous: REWIND and BACKSPACE Statements
Helen Rowlands
8/27/1998