Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

managing variables

Status
Not open for further replies.

fanta2

Technical User
Apr 10, 2005
78
CA
I have a lot of variables shared by different subroutines. How can these variables managed in a good way in fortran?
 
Which version of Fortran?

In FIV, F77, use named common blocks
In F90/95/2003 use modules.
 
Thank you xwb. It is F90. should I use only the declaration of variables in the modules or ...?
 
There are several ways of doing this.

One way would be to pass everything in as a parameter and not bother with globals. A bit idealist because you end up with tons of parameters like in the old days.
Adv: no globals
Dis: if one parameter is added, it has to be propagated all the way down to the routine that uses it.

Another way would be to create a type with all the "global" stuff in it. Declare one in the main module and pass it round as a parameter. This is more manageable. In this case all accesses to the globals will be prefixed with the item (say ggg%) so there is no need to prefix them yet again. This is basically an OO technique.
Adv: no globals
Adv: adding new variables is simple - just add it to the type and routines that use it.
Adv: doesn't suffer from propagation problems
Dis: all accesses must be prefixed with the type variable followed by %.

If you are sharing variables across several modules then make up one module with all the global decls. All the others will "use" this module. It would help if you identify the members of this module with some prefix like g_ so that anyone reading the code will know where it comes from and won't attempt to look for it locally.
Adv: adding a new global - just add it to the module and the routines that need it.
Dis: people maintaining the program must subscribe to adding the g_ prefix.

Note that on F90, all dynamic allocation must be done up front. It doesn't like allocatable parameters (at least the version I use doesn't).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top