Does Fortran preserve the value of internal variables through function and subroutine calls?

To answer your question: Yes Fortran does preserve the value of internal variables through function and subroutine calls.

Under certain conditions …

If you declare an internal variable with the SAVE attribute it’s value is saved from one call to the next. This is, of course, useful in some cases.

However, your question is a common reaction upon first learning about one of Fortran’s gotchas: if you initialise an internal variable in its declaration then it automatically acquires the SAVE attribute. You have done exactly that in your subroutines. This is standard-conforming. If you don’t want this to happen don’t initialise in the declaration.

This is the cause of much surprise and complaint from (some) newcomers to the language. But no matter how hard they complain it’s not going to change so you just have to (a) know about it and (b) program in awareness of it.

Leave a Comment