Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26734 Discussions

Local Variables Declaration and Initialization


This is a two part question.

  1. I been struggling to come up with a clear and concise way to handle the declaration of local variables. For reason I do not understand, any local variable given a value during its declaration is assumed to be SAVEd.In this case, I add the keyword “save” to make the declaration clear:

real(kind=dp), save :: climate = 0.0_dp

            so I assumed to declare a variable that is not SAVEd (to make it clear) I would use:

real(kind=dp), automatic :: temperature

I get the following message: warning #7410: Fortran 2008 does not allow this keyword. [AUTOMATIC].  Can someone explain this error and suggest another way to declare a variable as “not SAVEd”? I prefer not to depend on any compiler options. I have the warning for non-standard Fortran set for Fortran 2008.

  1. Does Fortran offer an easy way to initialize non-SAVEd variables everytime a subroutine or functions is called other than explicitly assigning a value at the start of the routine? For example, a keyword (in the source, not compiler) which would initialize all local variables to zero. Any other variables which needed a non-zero assignment could be added explicitly.The reason I ask is for large routines with many local variables the initialization section of the routine can be several pages long.Since this section is normally set once and ignored, it would be nice to find a way to minimize the amount of code required to initialize the local variables especially if the value to be set is zero.Any thoughts or suggestions would be welcome.

Local Variables Declaration and Initialization


0 Kudos
1 Reply

AUTOMATIC is an extension, not a standard part of the Fortran language, so when you ask for standards checking you'll get a warning for it.

By default, local variables do not have the SAVE attribute. As you note, adding initialization does imply SAVE. Earlier versions of the Fortran standard didn't specify that, but the current standard does. In the past, most implementations would treat initialized variables as SAVE anyway.

So just leave off the initialization and you'll be fine. No problem adding SAVE to the initialized variable - it's a helpful clue to the reader.

The Fortran language does not provide the kind of global initialization you ask for. Intel Fortran does have such a feature, enabled with:


You'll also want to add -init=arrays to get arrays zero-initialized. See our documentation on -init for more details.