If I initialize a variable in a Fortran declaration statement, that variable implicitly receives a SAVE attribute and the initialization expression will only be executed once.
For example, the following program
program test
implicit none
call foo()
call foo()
contains
subroutine foo ()
integer :: i = 0
i = i + 1
write(*,*) i
end subroutine foo
end program test
will print
1
2
Since this is different in many other languages I was wondering why the Fortran standard committee chose this behavior?
Thanks a lot!
Mike
This is mainly due to historical reasons. Old compilers (Fortran IV(66) and before) were implemented to produce programs using mainly static memory. Old machines even didn’t have any stack. Therefore the programs behave, as the variables were defined as
save.The predecessor of the variable initialization, the
DATAstatement, is more like defining an initial content of static memory (similar to the directives for the data segment in assembly), than the on call variable initialization you may know from C. The syntax became similar to the C variant later.