Thanks,
Cheng’an
Are you stuck with FORTRAN 77 or can you use Fortran 90/95?
In the latter case, use a module that contains these variables
and use them in the various routines.
If you are stuck with FORTRAN 77 for a good and solid reason ;) (*),
then COMMON blocks are most likely what you are looking for.
Put the declarations and the definition of the COMMON blocks
in a separate file so that you can use the INCLUDE statement
in each routine to, well, include these declarations and
definitions.
Regards,
Arjen
(*) There are several free Fortran 90/95 compilers available,
so not being able to buy one, is not really an excuse.
Note that you should initialise COMMON blocks in BLOCK DATA, and that
one of the commonest FORTRAN77 errors is to fail to link in BLOCK
DATA.
Incidentally, are you actually dealing with variables and not
constants ?
If the latter, and INCLUDE file containing appropriate type and
PARAMETER statements is the solution
Dave Flower
Hi Arjen,
Thanks for your reply and suggestions.
I have to use FORTRAN77. I forgot a very important thing in the last
post, i.e. I can not initialize these variables in simple way like
“length = 111”, instead I need to call a subroutine to get them out
from a “large database”. Common block can not work in my case. One
could say you can get them out once and reuse them repeatly later on,
but the problem is some of the variables could be changed by other
subroutines those are out of my control. Sync is necessary and should
be in the first step in my subroutines.
Thanks,
Cheng'an
But if they are always the same variables, you just need a SUBROUTINE
to either:
a) Get them out of the database (again)
or
b) Restore the contents of the COMMON block to the status quo (in
which case you will still need (a), and a routine to store the data
somewhere
Dave Flower
I am not so sure what you are trying to do, but...
If you have a large number of separate variables that you
need to pass around to many routines, you could put them in
a structure, though that requires features past Fortran 77.
For Fortran 77, you can EQUIVALENCE them to array elements,
and pass the array around. They must be the same type, though.
You can't EQUIVALENCE to dummy arguments, though.
Also, you can initialize variables in a DATA statement.
It isn't the same as an assignment statement, but DATA often
works better for initializing many variables (or arrays).
DATA I/1/,J/2/,K/3/,L/4/
or
DATA I,J,K,L/1,2,3,4/
note that DATA gives variables the SAVE attribute,
and that they are initialized only once.
-- glen
No, the F77 standard is quite clear - if a local variable, initialised
by a DATA statement is assigned a different value, it becomed undefind
on exit from the route. (17.3.6.b)
Dave Flower
Yes. Exactly.
What I need is
(a) a header file that declares all variables
(b) a subroutine that sync my variables to the latest status
Then, in all my functional subroutines, I can include the header file
and call the sync subroutine.
It works, :-)
Thank you all.
Cheng'an
EQUIVALENCE can avoid passing massive number of separated variables by
using array. However, an additional include file is necessary which
defines macros for all the separated variables. In this way, one will
not worry about situations like removing /adding a variable from/to
the array, which will change the array index for the separated
variables, in future development.
-- Cheng'an