Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SAVE, PROTECTED and PARAMETER for attributes module data entities

Tags:

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?

like image 908
Christoph90 Avatar asked Dec 19 '19 13:12

Christoph90


1 Answers

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.

  1. i may appear in a variable definition context whenever it is accessible.

  2. 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.

  3. 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.

like image 121
francescalus Avatar answered Oct 04 '22 00:10

francescalus