Let me add one more thing: Such a variable is initialized in the main programs and parameters (well, you should initialize them for parameters), but this may surprise you with your behavior if you are too used to using it and starting to use it in routines and functions:
For example, most of us initially assumed that this program:
program foo call bar call bar contains subroutine bar integer :: i=3 print '(A,I3)','At start of bar: i = ', i i = i + 1 print '(A,I3)','At end of bar: i = ', i end subroutine bar end program foo
will print
At start of bar: i = 3 At end of bar: i = 4 At start of bar: i = 3 At end of bar: i = 4
--- but it is not. He is typing
At start of bar: i = 3 At end of bar: i = 4 At start of bar: i = 4 At end of bar: i = 5
This is for “historical reasons”, as it often happens when they present behavior that seems manifestly wrong. Initializing a variable when declaring essentially turns this:
integer :: i
in
integer, save :: i = 3
and initialization is only performed for the first time . This means that the second time the variable remembers this previous value (4) and increases it.
Therefore, my reason for writing this is mainly to warn you not too comfortable initializing variables during declaration. I recommend doing this for the parameters and in the main program (where you will not encounter this problem, since you only enter the main program once) and a little more.
source share