Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

common data in fortran77

2 views
Skip to first unread message

tju329

unread,
Aug 20, 2008, 5:28:34 AM8/20/08
to
I have 50 different integer variables for different purpose (so they
are not stored in an integer array). After initialization, they will
be used in many subroutines. Then, I need to declare all these 50
variables, and initialized them locally in each subroutine. It’s
terrible to code in this way since it wastes many lines but do nothing
different, and the code is not beautiful. Could somebody suggest any
solutions?

Thanks,
Cheng’an

Arjen Markus

unread,
Aug 20, 2008, 5:45:17 AM8/20/08
to

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.

David Flower

unread,
Aug 20, 2008, 6:09:28 AM8/20/08
to

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

tju329

unread,
Aug 20, 2008, 6:22:01 AM8/20/08
to tju...@gmail.com

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

David Flower

unread,
Aug 20, 2008, 6:37:58 AM8/20/08
to
> Cheng'an- Hide quoted text -
>
> - Show quoted text -

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

glen herrmannsfeldt

unread,
Aug 20, 2008, 4:37:51 PM8/20/08
to

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

David Flower

unread,
Aug 20, 2008, 4:00:06 PM8/20/08
to

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

tju329

unread,
Aug 20, 2008, 11:50:45 PM8/20/08
to
> Dave Flower- Hide quoted text -

>
> - Show quoted text -

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

tju329

unread,
Aug 21, 2008, 12:03:45 AM8/21/08
to

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

0 new messages