The DATA statement

It is well-known that The DATA statement was implementend in FORTRAN 77. My question is: is it an obsolescent stattement in modern fortran and are there best alternatives to do the same?

thanks in advance

Albert

1 Like

@abach5 ,

It is simply not true and few hold the view that is consistent with your assertion here. ANSI X3.9-1966 document toward FORTRAN, arguably the first standard publication on this language that many came to see as FORTRAN 66, described in section 7.2.2 Data Initialization Statement the semantics of the DATA statement.

Secondly, no the DATA statement is not obsolescent, it is very much part of the language.

What is obsolescent is DATA statement among executable statements. Here the standard states,

As to “best alternatives”, consider first the predominant uses of DATA statement in codes that you will not view as “modern fortran”. Such uses will be:

  1. Data initialization of what are otherwise constants in code, take the value of the mathematical constant \pi for example,
  2. Data initialization of arrays that have some “heft” and pattern to them which then need to be reused in code.

For the use case #1 above, consider named constant facility in Fortran with the PARAMETER attribute.

For the second use case, consider the use of such data objects as module entities instead.

Note the standard semantics implicitly imparts the SAVE attribute to objects in DATA statements:

  • named constants with the PARAMETER are both immutable and available in program when a program unit containing its definition becomes active, Thus no issue here.
  • module entities are also SAVE and working with them can be seen as a modern, safer alternative.
1 Like

https://fortranwiki.org/fortran/show/array+constructors

is not specifically about the DATA statement, but talks about and shows newer initialization syntax as well as some pros and cons of the DATA statement.

Personally, I think the biggest con is that you do not have to initialize all the values, so if you wanted to the compiler will not warn you.

That very feature is a pro when you actually want to just initialize part of an array.

The values are initialized once, so changes applied to the variables between calls is cumulative. People not used to Fortran initialization being saved sometimes say this is a con. It is a useful feature when not unexpected.

DATA statements are often more legible, particularly when multi-dimensional variables are used.

Because they have been around a long time there are a lot of non-portable extensions for DATA statements. In particular, do not use the deprecated feature of allowing DATA statements to appear anywhere in the executable section of a procedure. Keep them in the declarations at the top. If you have COMMON blocks you have not been able to eliminate remember the standard requires COMMON block initialization in a BLOCKDATA when using DATA statements. Another common extension is to allow DATA statements in other locations to set COMMON blocks; and a lot of extensions exist that allow values to be declared multiple times. To me accidentally using an extension because of the long history is the biggest drawback.

For initializing scalars and vectors I use the new initialization syntax, which allows “everything” about the variable to be on a single statement.

I only use DATA statements in new code when large complex initializations are required, or where I want partial initialization.

Something like

    real,parameter :: a(3)=[10.0,22.0,33.0]

is nicer than something like

REAL A
DIMENSION A(3)
DATA A /10.0, 22.0, 33.0/

particularly when the statements may be scattered throughout the procedure.

And the PARAMETER attribute creates a real constant that can help with optimization and is immune to being accidently changed as a related bonus.

2 Likes

Sorry, I fixed a syntax error in a code in this thread, not realizing that would move it to the top of the forum.

1 Like