Why is there a SAVE tag in Fortran?
If I initialize a variable in a Fortran declaration statement, that variable implicitly gets the 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?
Thank you so much! Mike
source to share
This is mainly due to historical reasons. Older compilers (Fortran IV (66) and earlier) were implemented to create programs using mostly static memory. Older machines didn't even have a stack. Therefore, the programs behave because the variables were defined as save
.
The predecessor of variable initialization, the statement DATA
, is more like defining the initial contents of static memory (similar to data segment directives in an assembly) than variable initialization by call, which you may know from C. The syntax later became similar to variant C.
source to share