- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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?
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.....
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.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.....
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.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page