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!

I have a file of almost 5000 lines 1

Status
Not open for further replies.

garynewport2

Instructor
Jun 16, 2011
9
GB
I have a file of almost 5000 lines of code, made up of a series of subroutines.

I note that almost all the subroutines have the
Code:
include 'parm.h'
instruction. I can see the need of this between files but is this required in each subroutine contained within the same file?

The very first line of code is also
Code:
include 'parm.h'
; should this not suffice for the entire file?
 
Thank you, I was aware of the purpose of the contents and you are correct, it contains a series of variables required across the program. :)

My question is that in other languages you can include library files in the same way and the library contents are then available across the whole code, subroutines and all; everything contained within that file.
I was wondering if Fortran was the same. The link you shared was useful but wasn't clear on this. :(

Please do read the above with a smile; I cannot phrase the first line without it sounding condescending and that is NOT my intention at all. Hence the smiley face at the end (just in case). :)
 
This is a scoping problem. There is no "outer block" as such so including a file at the top level will not make the variables visible to everything below. Fotran's concept of global variables is common blocks. The libraries normally contain functions but not common blocks and declarations.

The other common use is module declarations but it is normally better to use an interface declaration even though it looks a bit long winded.
 
Resurrecting this one because I am exploring the use of parma.h and xvar.h further.

These two files contain definitions of a set of variables and common names for a group of variables; including arrays (1 and 2 dimensional).

Both files are called repeatedly throughout the one file, yet in each instance only a few of the variables are required.

I am unsure of the benefit of doing it this way and, from a space point of view, I can see a really good reason NOT to do it this way.

Looking at the one routine where they appear I can see the following...

Code:
C-
      include 'parm.h'
      include 'xvar.h'
      INTEGER     IE(ME+1),jp(6)
      CHARACTER*1 CS(2)
      CHARACTER*3 CE(ME+1)
      REAL*8      DE(6)
      DATA MODWRT/0/
      DATA CS/'*',' '/
      DATA CE/'HYD',' D ','Li ','He4','Stb',' C ',' N ',' O ','dG ',    &
     &        ' R '/
      DATA IE/1,2,3,4,8,5,6,7,9,10/
      DATA ifirst/0/
      save
C

Now, if the save is simply a declaration to cause ALL variables defined within this subroutine (regardless of where they appear) as static (and I am assuming this means those included within the 2 files) then this means that a whole series of variables that are not even used in this subroutine are created and stored. Surely the following is true...?

[ul]
[li]the save command would be best placed at the beginning, since its position has no real relevance and it shows clear intent from the outset?[/li]
[li]I should shift into the subroutine only those variables that are required by the subroutine into the subroutine itself; reducing the number of variables being stored in memory?[/indent][/li]
[li]If common means common across the whole program then a better way of conveying data from one subroutine to another is through the use of parameters?[/li]
[/ul]

Sorry if this is a lot of questions for one query but I'm really keen to explore the concept of Fortran as quickly as possible and re-develop this code, since I am convinced it is wasting space and time.
 
Save saves all local variables. If the variables defined in the .h files belong to common blocks, then they are not 'saved'.

In the old days (1950s-1970s), there were 2 implementations.

1) When a subroutine/function was re-entered, all the local variables had random values
2) When a subroutine/function was re-entered, the values of all the local variables were 'remembered' from the last run (static variables in C, own variables in Algol 60). Data statements are only applicable when the subroutine is first called.

In F77, by default, #1 applies. Use save for #2

There isn't any rule on where save should be placed. You can specify variable names after the save to indicate that those are the only ones to be saved e.g.
Code:
save CE, ifirst
would only save CE and ifirst. Save has no effect in the main program.

Common doesn't mean common across the whole program. It means common across the subroutines that have it. For instance you could have
Code:
subroutine AB
common /A/ ...
common /B/ ...
! C not required
...
end subroutine AB

subroutine BC
! A not required
common /B/ ...
common /C/ ...
...
end subroutine BC

subroutine CA
common /C/...
common /A/...
! B not required
...
end subroutine CA
Later versions of Fortran no longer use common blocks. They use module level variables instead. This can be quite confusing as it is like statics in classes in C++. There are no "instances".

It is swings and roundabouts when you start playing with module level variables vs parameters vs type variables. I prefer using type variables - bung everything in the variable and pass it around. The subroutines can pick up whichever fields they require.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top