Does a common block in Fortran serve the purpose of global variables?
If this is the case, then can these values be modified by a subroutine?
Will SAVE
store the modified parameters from within a subroutine for uses other than from inside the subroutine?
[Edit: I'm basing the answer on understanding that the question is about
SAVE
on a common block, not on local variables (ie., as an alternative forCOMMON
). Otherwise see cup's comment.]Common blocks were indeed the approach for "global" variables in Fortran 77. The variables within the block can be modified in a subroutine and the changes will be visible elsewhere (see caveat).
SAVE
doesn't directly change the ability of other subprograms to use the modified variables. Instead its purpose is to ensure that data in the block don't become undefined when the block goes out of scope. Note that ifSAVE
is present in one subprogram it must be present on the block in all subprograms where the block features (but not necessarily the main program).From memory this means that (this isn't really F77; for the concept):
won't be well-behaved. [Whether real-world compilers make you pay is debatable.]
Variables in a common block cannot have the
SAVE
attribute applied separately, and having the attribute on a local variable doesn't change its accessibility. So, for example, aSAVE K
in one subroutine wouldn't make that variable accessible anywhere else. What will happen, however, is that it retains its value after control returns from there (for the next time the subroutine is entered).Finally, there are better approaches after Fortran 77.