# Use of the statement "contains"

**URL:** <https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909>\
**Category:** Uncategorized\
**Created:** [June 3, 2023, 9:57am UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909 "2023-06-03T09:57:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/patrick/32/3170_2.png) [@Patrick](https://fortran-lang.discourse.group/u/Patrick)\
**Post date:** [June 3, 2023, 9:57am UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/1 "2023-06-03T09:57:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 3, 2023, 10:08am UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/2 "2023-06-03T10:08:34Z")

</div>

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:

```fortran
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:

```fortran
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.

---

<div class="post-metadata">

**Author:** ![Patrick](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/patrick/32/3170_2.png) [@Patrick](https://fortran-lang.discourse.group/u/Patrick)\
**Post date:** [June 3, 2023, 11:37am UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/3 "2023-06-03T11:37:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 3, 2023, 12:34pm UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/4 "2023-06-03T12:34:59Z")

</div>

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

Every Fortran program is divided in “program units”

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/3/31ad53231ab7c4cd0bf1bd7d6b1482b9aa5a32e7.png)

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:

```fortran
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.

---

<div class="post-metadata">

**Author:** ![Patrick](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/patrick/32/3170_2.png) [@Patrick](https://fortran-lang.discourse.group/u/Patrick)\
**Post date:** [June 3, 2023, 12:49pm UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/5 "2023-06-03T12:49:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 3, 2023, 1:03pm UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/6 "2023-06-03T13:03:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Patrick](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/patrick/32/3170_2.png) [@Patrick](https://fortran-lang.discourse.group/u/Patrick)\
**Post date:** [June 3, 2023, 1:34pm UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/7 "2023-06-03T13:34:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 3, 2023, 2:31pm UTC](https://fortran-lang.discourse.group/t/use-of-the-statement-contains/5909/8 "2023-06-03T14:31:07Z")

</div>

🙂
