If I want to prevent module data from being changed during program execution, I seem to have at least three options in Fortran:
1. using the SAVE statement
module mymod
implicit none
save
integer :: i = 1
end mymod
2. using the PROTECTED attribute
module mymod
implicit none
integer, protected :: i = 1
end mymod
3. using the PARAMETER attribute
module mymod
implicit none
integer, parameter :: i = 1
end mymod
What are the differences and implications of the three options?
This answer addresses the non-subtle aspects of the use of the entities named i. There are a few other considerations to be made in more complicated cases. It also uses the term variable definition context. Loosely speaking, this means where a variable may appear such that its value could change. This would be things like being the left-hand side of an assignment; appearing as a do variable or corresponding to an intent(out) argument.
i may appear in a variable definition context whenever it is accessible.
i (as a non-pointer object), where it is accessible, can appear in a variable definition context only in the scope of its module or descendants of that module.
i can never appear in a variable definition context: it is a constant not a variable.
The save attribute (in the current standard any module variable has this attribute; even i in the second example is saved) does not control modification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With