2

When working with some legacy code, I've found the following Fortran function declaration. The snippet below shows both the function declaration and the declaration of parameters. I believe that Fortran is a case-insensitive language.

 SUBROUTINE CLIP2G (fcut,TIME,NUMS,NUMG,CLIPG,CLIPGL,CLIPGR,MODE,PHZ)

      real fcut, TIME,
      integer NUMS, NUMG
      DIMENSION CLIPG(1)
      REAL clipgr(1),clipgl(1)
      INTEGER MODE
      LOGICAL PHZ

What is the meaning of the DIMENSION CLIPG(1) statement?

I found a link to a rather concise explanation of the statement, but primarily as a C/C++ programmer, I find the concept somewhat challenging to understand. Note how REAL clipgr(1), clipgl(1) are followed by a bracket (1). Is this an array of length = 1 with type REAL?

There are some other links on Stack Overflow, but even in the C-like syntax given in the posting linked below I am uncertain with respect to the meaning.

Fortran Function explanation

Perhaps DIMENSION CLIPG(1) is equivalent to the REAL CLIPG statement? What is the closest C-language equivalent?

Community
  • 1
  • 1
Nicholas Kinar
  • 1,440
  • 5
  • 24
  • 36

1 Answers1

6

DIMENSION is used to indicate to the compiler, that the variable is an array. In this case DIMENSION CLIPG(1) declares CLIPG as being an array of one element. It is also implicitly typed to be REAL, so an equivalent declaration would be:

REAL CLIPG(1)

The equivalent C construct would be

..., float clipg[1], ...

Note that this might not mean that CLIPG is really an array of just one element. It could also be used as (a very bad) way to explain to the compiler that CLIPG is an array of a varying size (e.g. you can call this subroutine once with an array of 5 elements and then again with an array of 50000 elements). It is declared as having just one element, so the compiler knows that it is an array, but then it could be accessed way beyond its end if the actual argument is a larger array. This is a really bad practice but you can find it used in lots of very old Fortran codes.

FORTRAN 77 provides another way to describe such arrays:

REAL CLIPG(*)

or

DIMENSION CLIPG(*)

Such arrays are called assumed-size arrays. Only dummy routine arguments can be declared as assumed-size arrays and only the last dimension of the array could be omitted, e.g.

DIMENSION CLIPG2D(10,*)

(but not DIMENSION CLIPG2D(*,10))

This means that CLIPG2D is a something x 10 matrix (Fortran stores matrices columnwise), and something could vary. Whenever assumed-size arrays are used, one also has to explicitly supply the size of the omitted dimension to the routine.

In C one would use either float clipg[] or float *clipg.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • You are absolutely right. Examining the code, it appears that `DIMENSION CLIPG(1)` is being used as an array of varying size. The size is given by `NUMG`. Thanks for such an astute answer. – Nicholas Kinar Nov 23 '12 at 18:43
  • 1
    @NicholasKinar, note that this could lead to compiler warnings or errors if a more strict compiler is used. If it happens so, just replace `(1)` by `(*)` and it should do the trick. Or you are porting the code to C? – Hristo Iliev Nov 23 '12 at 18:46
  • Yes - I was just doing that, Hristo, and no, I am not porting the code to C just yet :-) BTW, would it be better to use something else other than assumed-size arrays? – Nicholas Kinar Nov 23 '12 at 18:48
  • You could also say: `REAL CLIPG(NUMG)`. – Hristo Iliev Nov 23 '12 at 18:57
  • Re the "better" question: Modern Fortran would use assumed shape arrays -- declared with a colon. This causes the compiler to pass an internal structure with the shape information. P.S. A related previous question showing a drawback of assumed size -- that runtime subscript checking is not possible: http://stackoverflow.com/questions/9840693/why-no-runtime-error-when-clearly-writing-over-array-bounds – M. S. B. Nov 23 '12 at 20:41
  • @M.S.B., now you have to explain explicit interfaces too :) – Hristo Iliev Nov 23 '12 at 20:47