Passing MIN to a function

While reading OLD code I came across the following function:

FUNCTION SYM(N)
IMPLICIT DOUBLE PRECISION (A-H, O-Z)
SYM = 1.D0
IF((N/2)*2-N.NE.0) SYM=-1.D0
END FUNCTION SYM

Which was later used as follows:

RODR=RODR*SYM(MIN)

The compiler complained that the ‘Mask’ argument of ‘SYM’ intrinsic must be LOGICAL. The problem is, that I can’t figure out what it means to pass the function MIN to another function that only returns 1 or -1. It seems bs to me.

MIN gives the most negative entry in an array so I assume it just means “use ‘-1’ here”. But why not just write “-1” or “1”?

You would probably need to post more of the code for anyone to determine everything that is going on, but I’ll make a few guesses in case they help. I do not recognize SYM() as a standard fortran intrinsic. Maybe there is a new intrinsic added that I don’t recognize. One possibility is that your compiler has added SYM() as an extension.

So the simplest fix would probably be to add

double precision, external :: sym

to your code. That tells the compiler that sym() is an external function and that it returns a double precision value. In f77, you would need to split that line into two separate lines, one for the external declaration and the other for the type declaration. That latter one might be unnecessary with implicit typing, but it would not hurt to add it anyway to make it clear to future programmers what is expected.

With modern fortran, you could go further and add an explicit interface to sym(). You could do this with an interface block, but a better way might be to add the sym() function to a module and then USE that module in your code. If sym() is referenced in many places, then you would need to add the USE statement everywhere it is necessary. If it is used in only one place, then another possibility is to make sym() an internal procedure. That also provides an explicit interface, and it does so without contaminating the global namespace.

The fact that MIN is an intrinsic function name is a red herring. It is almost certainly a simple integer variable in your code, probably not declared because of implicit typing. You could add the declaration of MIN to make that clear to future programmers too.

1 Like

Perhaps MIN is a variable. How old is OLD? The generic MIN function wasn’t in the Fortran 66 standard. It was introduced in Fortran 77.

While checking I notice that the Fortran 66 standard is 36 pages. The front matter of the 202x standard is 14 pages. By the 36th page we are approaching the end of the Terms and Definitions.

Works perfectly!

I suspect, although I am not certain, mid to late nineties.