Search code examples
fortran

What is the difference between "data" versus "=" in Fortran?


I am new to Fortran and I'd like to understand the differences between these two statements:

logical :: TF
TF = .true.

and:

logical :: TF
data TF/.true./

Please keep the response in more simple to words, I've recently moved from Python to this compiled language.


Solution

  • Loosely, a DATA statement performs explicit initialization of a variable. An assignment statement gives a variable a value.

    These are very different things. You can consider the DATA statement version of this question like

    logical, save:: TF=.TRUE.
    

    and see many questions/complaints about how this behaves unexpectedly.

    In general, it's worth noting that DATA statement and the "declaration with SAVE" (actually an initialization on declaration) are also (subtly) different. As a beginner, perhaps pretend these initializations don't exist and stick to assignment.