Scope in fortran-77

Hello all,

I would like to understand how scopes work in Fortran-77.

From what I have understood so far, there is no global scope in Fortran-77. However, the “common block” allows this to be changed as it allows variables to be shared between multiple program units.

I have come across examples of code where the “#include ‘file.inc’” directive (not to be confused with the Fortran include statement) is also used to group together a number of declarations which are then included in the declaration part of each program unit.

for example: in declarations.inc I have this

      integer x
      real y
      complex z

and in main.F I have this

      program test
#include "declarations.inc"
      x = 1
      y = 2
      z = cmplx(3, 4)
      print *, 'in test: x=', x, ', y=', y, ', z=', z
      call sub(z, y, x)      
      end
      subroutine sub(z, y, x)
#include "declarations.inc"
      x = 10
      y = 20
      z = cmplx(30, 40)
      print *, 'in sub : x=', x, ', y=', y, ', z=', z
      end

And even with this construction, the scope always remains local to each program unit.

Is it not possible for a program to contains another subprogram? And then have variables defined in the scope of its parent? What about the ENTRY statement, which seems to behave almost identically to the contains statement in modern Fortran subprograms?

Thanks

You have to declare your variables to be COMMON in declarations.inc.

Thanks for your reply.

In fact, the includes I’m dealing with are of this form (the same in the declarations.inc file), it’s not always that they contain common block.

Maybe my question isn’t very clear. I’m trying to analyse the source code, including the scope of each variable. In which program unit is it declared, in which program units is it visible, where is it modifiable, where is it modified, that kind of thing, …

To do this, I need to understand the scopes in Fortran-77.

As indicated in my first post, as far as I know, scopes are local to each program unit (program, subroutine, function), but the common block changes this behaviour and allows you to have a kind of global scope (I am looking for confirmation).

My other question concerns the ENTRY statement, which can (maybe) also change the behaviour of scopes.

Yes, you are correct about the scope of variables in f77. Argument association allows variables defined in one subroutine to be modified by another subroutine. Common blocks also allow storage to be shared by multiple subroutines; common blocks share memory, not variables, so beware of that distinction.

ENTRY can be used to share data within a subroutine among its entry points. SAVE is used to keep local variables defined from one subroutine call to the next, or from one entry point to the next, so it also affects scope in f77.

You will find quirky restrictions with all of these. For example with argument association, the programmer is not allowed to modify dummy arguments that are aliased to the same actual argument or to a variable that is also accessible in a common block. With entry points, one entry point cannot call another entry point, but the programmer is allowed to branch (e.g. with goto, end=, arithmetic if, etc.) anywhere within the subroutine; the programmer is allowed to reference only the dummy arguments associated with the active entry point (some compilers allowed this restriction to be violated, so watch out for that usage too in legacy code).

Thanks for the answer, it’s already given me a clearer idea of what I was thinking.

For example, I came across this link which explains scopes in Fortran-90. I haven’t found such a document for Fortran-77, so this might be an opportunity to make one for Fortran-77.

Anyway, thanks for the reply. I’ll use it as a basis for reflection and research, or even to clarify questions on particular points.

Hallo,
just for couriosity. Is there a reason why you have to stick to Fortran 77?

The reason is that I work on legacy code to do analysis. And part of the code base I’m working on is Fortran version 77.

That why I’m not necessarily comfortable with Fortran, especially an old version like this one.

(Emphasis mine) Which means you could start using more modern features. Just because the code is in FORTRAN 77 doesn’t mean the compiler you’re using won’t support modern features. That means you can incrementally “modernise” the code (I do this all the time). Granted, understanding the existing code is the first step. Hopefully the answers will help you leave it better than you found it.

3 Likes