next up previous contents
Next: BLOCK DATA Statements Up: Initialisation of Variables and Previous: Initialisation of Variables and

DATA Statements

One of the tasks a compiler has to perform when converting a source program into machine code is to assign memory locations for all variables and arrays. However it does not, by default, store values in them and so, when the program starts to run the variables and arrays will initially have what ever values happen to be in the memory reserved for them. On some, but not all, computers memory is 'cleaned' to zeros before starting but a programmer should never assume that this is so. To initialise memory the DATA statement is used for example :-
DATA ICOUNT,PI/0,3.14159/
A DATA statement consists of a list of variables, arrays and array elements terminated by a / followed by a list of values to be stored in them. In the above example ICOUNT would be initialized to 0 and PI to 3.14159. The above DATA statement is not the same as:-
ICOUNT=0
PI=3.14159
for these are executable statements (see section 18) and reset ICOUNT and PI each time they are encountered where as the DATA statements have no effect while the program is running. For example, consider these two subroutines:-

 
        SUBROUTINE A
         
        DATA ICALL/0/
         
         
        ICALL=ICALL+1
        PRINT *,ICALL
         
        RETURN
        END
and
 
        SUBROUTINE B
         
        ICALL=0
         
         
        ICALL=ICALL+1
        PRINT *,ICALL
         
        RETURN
        END
Each time subroutine A is called it will type out an integer one higher than the previous time starting with the value 1. Subroutine B will always type out the value 1. Sometimes a routine needs to have code that is executed when the routine is called for the first time; DATA statements make this easy, for example:-

 
        SUBROUTINE INPUT
         
         
        LOGICAL FIRST
         
        DATA FIRST/.TRUE./
         
         
        IF (FIRST) THEN
         
        .
        .    initialisation code
        .
         
          FIRST=.FALSE.
        ENDIF
         
        .
        .
        .

Array elements and complete arrays can also be initialised using DATA statements. The list of values in a DATA statement may also contain terms of the form :-

n*value
where n is positive integer and is equivalent to a list of n values. For example:-
DIMENSION A(7,5),B(4)
DATA A,B(3)/36*0./
This initialises every element of A and the element 3 of B to zero. Part of an array can be initialised using the implied DO LOOP construction described in section 10.2.2. For example:-

 
    DIMENSION C(2,3)
    DATA (C(1,I),I=1,3),(C(2,I),I=1,3,2)/3*1.0,2*2.0/


next up previous contents
Next: BLOCK DATA Statements Up: Initialisation of Variables and Previous: Initialisation of Variables and
n west (APC)
2000-03-08