Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

Confusion re initailization of variables

WSinc
New Contributor I
364 Views
Hello;;

I was under the impression (perhaps mistaken) thet variables in a subroutine do not keep
their values each time it is called, unless the variable is STATIC.

This example says otherwise. When I run it, the value is changed each time I call it.

do

call sub1()

enddo

end

subroutine sub1()

integer*4 i

i=i+1

print *,i

end

Also, initializing I with a data statement does not make any difference. What if I WANT it to be the same every time? Is there an article somewhere about this topic?
How does this work if the variable is an ARRAY?

0 Kudos
5 Replies
dannycat
New Contributor I
364 Views
If you want the subroutine to keep the value of i you need to declare i withthe SAVE attribute:

integer,save :: i = 0

This will initialise i with a value of0 and keep incrementing it and saving it each time the subroutine is called. The Intel compiler,by default, does not assign the save attributeto local variables unless the /QSave compiler directive is used. If you want the value of i to be initialised every time this should be done using

i = 0

Uninitialised variablesare the cause of many bugs in FORTRAN so it is good practice to explicitly initialise local variables in subroutines and functions and not assume they will be initialised to 0.
0 Kudos
WSinc
New Contributor I
364 Views
What confuses me is: in the example I gave, it updates the value of I every time, without using the "save" option.

I am wondering if the SAVE option is a default.....
What is the difference between SAVE and STATIC?

I normally do explicitly initialize variable each time, like you suggested.
Apparently arrays DO get saved by default.....
0 Kudos
TimP
Honored Contributor III
364 Views
Quoting billsincl
What confuses me is: in the example I gave, it updates the value of I every time, without using the "save" option.

I am wondering if the SAVE option is a default.....
What is the difference between SAVE and STATIC?

I normally do explicitly initialize variable each time, like you suggested.
Apparently arrays DO get saved by default.....

No, there's no guarantee that the data will be over-written. They would likely be over-written if you would call another subroutine in between.
0 Kudos
jimdempseyatthecove
Honored Contributor III
364 Views
SAVE is equivilent to C/C++ static
AUTOMATIC (Intelnon-standard attribute)is equivilent to C/C++ on-the-stack

Omission of SAVE (or AUTOMATIC) use default rules which vary depending on

type (predefined or user defined)
scalar or vector(array)
allocatable or not
pointer to scalar or user defined type
pointer to array
compiler option (e.g. -openmp)
attribute of RECURSIVE on subroutine or function
compiler vendor's choice of placement when not explicitly stated in standard

Be careful about making assumptions.

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
364 Views
Which compiler are you using? CVF would make local variables SAVE by default, Intel Fortran does not. The Fortran standard requires the user to give the SAVE attribute to variables you want to keep their value across calls. Using DATA provides a one-time initialization only (and implies SAVE). For simple variables, the only way to have them reinitialized on each call is to explicitly assign to them - DATA won't work. The exception to this rule is variables of a derived type where the type has default initialization. In this case, the initialization is done on each routine entry.
0 Kudos
Reply