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

initialize Variable in declaration section of a Subroutine

Ho__Matthias
Beginner
413 Views
When I initialize a Variable in a Subroutine within the declaration section, the initialization is just executed in the first run of the Subroutine. In the second run, the old (saved?) value is used.
As a minimal example I have the following code with the output 1 and 5 instead of two times the 1.
Does someone understand this feature?



Program Test
Implicit none

Call Sub1( )
Call Sub1( )

contains

   Subroutine Sub1( )
   Implicit None
   Integer*4 :: j1=1
   Print*, j1
   j1=5
   End Subroutine Sub1

End Program Test

 

 

0 Kudos
2 Replies
Ho__Matthias
Beginner
413 Views
Sorry for the wrong formatting! The text again:

When I initialize a Variable in a Subroutine within the declaration section,
the initialization is just executed in the first run of the Subroutine.
In the second run, the old (saved?) value is used.
As a minimal example I have the following code with the output 1 and 5
instead of two times the 1.
Does someone understand this feature?
TimP
Black Belt
413 Views

The initializer has effect only the first time.  This has been a feature of the standard since this form of initializer was adopted (f90).  Declaring with initializer also has the effect of SAVE, so the value upon leaving the procedure remains visible on reentry.  Using the non-standard extension integer*4 doesn't make the compiler revert partially to the behavior of some compiler from the early days of this extension. If you set a -stand option, you should get a warning (but not about the compiler not mimicking an old extended Fortran 66 behavior).

Reply