Use of the statement "contains"

I am aware that the “contains” statement goes directly below the statement "end program ‘whatever’ ". In the literature, one gets treated to half an hour of text on the subject. So my question is: Is the concise essence, that “contains” prompts the compiler to check the consistency of the data items used?

Well, no.
You can have internal procedure after a contains if you are inside a procedure or a program unit; or module procedure if you are in a module; while it has a different meaning inside a type declaration.
Examples:

program my_program
implicit none
  print *, my_internal_fun(7)
contains
  integer function my_internal_fun(x) result(r)
    integer :: x
    r = 2*x
  end function
end program

Now, as it is declared as an internal procedure it has a explicit interface, that is when it is called the compiler checks that the arguments are correct.
But that will be the same if it were a module procedure:

module my_module
implicit none
contains
 integer function my_internal_fun(x) result(r)
    integer :: x
    r = 2*x
  end function
end module

program my_program
use my_module
implicit none
  print *, my_internal_fun(7)
end program

Or you get the same thing if you specify an interface.
The actual point is that you have an explicit interface. More over with an explicit interface you can have assumed shape dummy arguments, etc.

1 Like

Are you trying to tell me that wherever the word “contains” appears, the complier will check the consistency of the following logic with its environment?

No,
“contains” means just contains like in English.

Every Fortran program is divided in “program units”

Every program unit is compiled on its own. It can use other modules, in this case it can access information present in the used modules.
If a procedure is called and that procedure has an explicit interface the compiler can check that it is calling correctly that procedure.

All procedure present inside a module have an explicit interface inside that module (so yes, that procedures must appear after the contains statement). And that procedures may be called by other procedure only by using that module (directly or indirectly) and thus having access to the explicit interface.

An internal procedure may appear after a contains statement present inside a procedure, it is accessible only from the procedure that contains it, and yes it has an explicit interface.

By the way, I can write, but is better not to:

subroutine extproc(a)
  integer, intent(in) :: a(:)
  print *,size(a)
end subroutine

program my_program
implicit none

interface
  subroutine extproc(a)
    integer, intent(in) :: a(:)
  end subroutine
end interface

call extproc([1,2,3,4])

end program

As you see I have declared an explicit interface to the external procedure extproc, and I was even able to pass it an assumed shape array, and everything worked because the compiler was able to check the interface.
And no contains is present.

Don’t exchange the cause with the effect.

Having a procedure in the contains part (after a contains) means that that procedure has an explicit interface. But it is due to the explicit interface that the compiler can check that the actual arguments are compatible with the dummy arguments.

Are you saying “Yes”? Except when there is an explicit interface being used?

Well, there are a lot of things that the compiler can check.

I’m saying that the interface to a procedure is checked if the procedure has an explicit interface.

That means: place every procedure that you write inside a module, and for some specific use inside another procedure. So basically after the contains statement.

There are other important concepts like the “use association” and “host association” to be aware of.

Which other languages do you know?

Removed. Sorry. The answer to your question was considered out of bounds.

:slightly_smiling_face: