gansub
gansub

Reputation: 1174

gfortran reports no compilation error when function variables are not declared

This is a fortran 90 function that I have within a main program. As you can see several variables types such as ZLAMS are not declared at the top. But gfortran 5.2 does not report any error whatsoever. If however I move this code to a separate module and then call this function in the main module all the undeclared variables report a compilation error. Why ?

REAL(kind=sp) FUNCTION ABCTEST (PHIS, LAMS, POLPHI, POLLAM)
use k_parameters,ONLY:&
   sp

REAL(KIND=SP)        LAMS,PHIS,POLPHI,POLLAM

DATA        ZRPI18 , ZPIR18  / 57.2957795 , 0.0174532925 /

SINPOL = SIN(ZPIR18*POLPHI)
COSPOL = COS(ZPIR18*POLPHI)
ZPHIS  = ZPIR18*PHIS
ZLAMS  = LAMS
IF(ZLAMS.GT.180.0) ZLAMS = ZLAMS - 360.0
ZLAMS  = ZPIR18*ZLAMS
ARG     = COSPOL*COS(ZPHIS)*COS(ZLAMS) + SINPOL*SIN(ZPHIS)

ABCTEST = ZRPI18*ASIN(ARG)

RETURN
END FUNCTION ABCTEST

Upvotes: 1

Views: 344

Answers (1)

You must use IMPLICIT NONE at the top of each compilation unit to get such error. Otherwise, the implicit typing rules are in effect.

No other type of the implicit statement except of IMPLICIT NONE is recommended in modern Fortran. It should be used in all modern code.

Each compilation unit means every external procedure, main program and each module or submodule. Module procedures will see the module's implicit statement due to host association. The same holds for internal procedures that are controlled by the host procedure's implicit statement.

Upvotes: 3

Related Questions