What does this data reference mean?

I have in the code I have acquired the following two lines…

      data ifirst/0/
      data zMmin/9.945d31/

What does this mean?

It appears to be initialising two variables (ifirst = 0, zmmin = 9.945d31) but what does the data part mean?

Yes, it is initializing those two variables with those values, but the data part is saying “do the initialization before program startup”. I.e. the program will start execution with those variables having those values, but they may change as the program executes. Thus data does not imply parameter, which is a common misconception I often see. If you want constant data, you should switch to

integer, parameter :: ifirst = 0
double precision, parameter :: zMmin = 9.945d31
3 Likes

The DATA statement was important in the Fortran IV and Fortran 77 era. Now we have other ways of initializing variables, and in a language with a long life you should expect to see some redundancies in features.

For a good explanation of the DATA statement, see the Xerox Sigma Fortran IV manual.

A statement in that manual has been twisted to serve as a derogatory joke.

1 Like

I don’t (yet).

I really should get one.
I have a PDF from one of the professors, who used to teach Fortran and I found this quite useful. However, I struggle to read from PDFs.
I will look in to getting the Metcalf et al book. Thank you. :slight_smile:

Would it be best to get the Modern Fortran Explained (Numerical Methods and Scientific Computations) version, considering that I am going to shift the code to F90?
Or go for the Fortran 95/2003 Explained (Numerical Mathematics and Scientific Computation) instead; since I am currently working with the old version?

No, the values do change, so data seems correct.
I just can’t see why I don’t simply do…

integer :: ifirst = 0
double precision :: zMmin = 9.945d31

Or does this extend the availability of the variable outside of the routine that defines it (in this case, as the program first starts)?

One thing I can see is that almost the next line states:

if (ifirst .ne. 0) then

So, unsure if the value is kept if the program is run a second time (or more), or if it is reset to zero each time the program hands back control to the calling program.

Yes, I’m discovering that there is a lot of redundancies! Hahaha!

I can’t currently access your link from XEROX (I’m behind a firewall that is preventing me) but what would I consider replacing it with?

I bought all three (latest, not latest, old) - just in case! :laughing:

Variables declared inside a procedure are never accessible outside that procedure. Initializing a variable on it’s declaration implicitly gives it the save attribute, meaning the variable will retain its value between invocations of that procedure. I.e.

integer :: i = 0
! do some stuff that refers to i
...
i = 2
end procedure

The second time you call that procedure, i will start with the value 2, not 0. This is one of those gotchas that almost every Fortran programmer gets caught by at some point.

Values are never “remembered” between executions of a program, unless explicitly written to and then read from a file.

Yes, sorry, poor use of language. I didn’t mean to imply that the program could recall values between executions but between calls from another routine (as in the case of subroutines).

Given what you are saying, there is no purpose, therefore, to use the save Command, if all variables are declared?

If so then, yes, that does seem to be a peculiarity of the language; though a logical one, to a degree.

Thank you.

There is the SAVE attribute and the SAVE statement. The SAVE statement can be used to give the SAVE attribute to a variable. This is necessary for implicitly typed variables.

SAVE thisvar

Another way to give the SAVE attribute to a variable is to include the SAVE attribute specifier in the declaration of the variable.

REAL, SAVE :: thisvar

Another way is to initialize the variable

REAL :: thisvar=1.

This is not an assignment, it is initialization. And you are invited to explicitly use the SAVE attribute specifier, so that you remember that fact

REAL, SAVE :: thisvar=1.

Of course, someone might later be tempted to remove the SAVE attribute specifier imagining that they are removing the SAVE attribute from the variable, you can’t win!

Perhaps better is

REAL :: thisvar=1. ! implicitly SAVEd

which shows the last programmer was aware that thisvar has the SAVE attribute and the next programmer will have to learn what that means.

Many people, including me, expressed support for making implicit save obsolescent and eventually deleting it from Fortran, in the thread Fortran function remembers values (newbie help), and I would not advise people to write code that could be declared obsolescent in the future. Since

REAL, SAVE :: thisvar=1.

is much less likely to be removed from Fortran, I suggest using that instead.

This is amazingly useful, thank you.

I have gone for the latter option (as per Beliavsky) and seeing how this all goes.

I am so grateful for the astounding help given. :slight_smile: