Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put `implicit none` in Fortran

Do I need to put implicit none inside every function and subroutine?

Or is it enough to put it at the beginning of the module containing these functions and subroutines?

Or is it enough to put it at the beginning of the program that is using these modules?

From observation of other's working code, implicit none is included in all these places. I am not sure if this is done redundantly because removing implicit none from subroutines still compiled and produced the same output.

By the way, I am using gfortran fortran 90.

like image 440
Fabricator Avatar asked Jun 21 '14 01:06

Fabricator


People also ask

Why do we write implicit none in Fortran?

The IMPLICIT NONE statement disables all implicit typing defaults. When IMPLICIT NONE is used, all names in a program unit must be explicitly declared. An IMPLICIT NONE statement must precede any PARAMETER statements, and there must be no other IMPLICIT statements in the scoping unit.

What does implicit mean in Fortran?

An IMPLICIT statement specifies a type and size for all user-defined names that begin with any letter, either a single letter or in a range of letters, appearing in the specification. An IMPLICIT statement does not change the type of the intrinsic functions.

How do I use subroutine in Fortran?

A Fortran function is similar to a mathematical function, which takes one or many parameters as inputs and returns a single output value. A Fortran subroutine is a block of code that performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified.

What is real in Fortran?

The REAL statement specifies the type of a symbolic constant, variable, array, function, or dummy function to be real, and optionally specifies array dimensions and size, and initializes with values.


1 Answers

The implicit statement (including implicit none) applies to a scoping unit. Such a thing is defined as

BLOCK construct, derived-type definition, interface body, program unit, or subprogram, excluding all nested scoping units in it

This "excluding all nested scoping units in it" suggests that it may be necessary/desirable to have implicit none in each function and subroutine (collectively, procedures) defined in a module. However, inside procedures contained within a module there is a default mapping based on the host scoping unit. So, with implicit none in the module it isn't necessary to have that in contained procedures.

This host scoping unit rule applies equally to internal programs. This means that implicit none in the main program covers all procedures contained in it; and that the same applies for internal programs of module procedures. Block constructs see this also, and the implicit statement isn't even allowed within one of these.

However, external functions/subroutines will not inherit implicit behaviour from a program or module, and modules don't inherit it from programs/other modules which use them. This clearly makes sense as the implicit typing must be known at compile time and be well defined regardless of their ultimate use.

Further, one cannot apply implicit rules from one program unit to a module it uses, such as in:

implicit none use somemodule  end program 

An implicit statement must follow all use statements.

Equally, a submodule is a program unit in itself, distinct from its ancestors. A module or submodule is a parent, not a host, of a submodule which extends it: the host scoping unit rule doesn't apply and the submodule doesn't inherit the mapping rules from its parent. Without an implicit statement in the submodule's scope the default rules will apply there.

The host scoping unit rule notably doesn't apply to interface bodies. IanH's answer motivates that exception, but it's an important enough thing to re-stress. It has caused much confusion.

module mod  implicit none    interface     subroutine external_sub()       ! The default implicit typing rules apply here unless       ! there is an implicit statement, such as implicit none.       ! Those from the module aren't in force here.     end subroutine   end interface  end module 

Regarding the test of removing implicit none from a subroutine: if the code is valid with implicit none then it must be valid and identical without that statement. All entities must be explicitly declared in the former case, so no implicit rules would be applied in the latter.

like image 111
francescalus Avatar answered Sep 19 '22 14:09

francescalus