1

I have written some code in Fortran but not really gotten confident in how to structure longer codes.

I often want assumed-shape arrays. This seem to be possible when using an interface block, a module or a contains statement. However having the subroutine outside of the main code, without using any of these methods works for many cases, but not for assumed-shape arrays.

In what context is each of these approaches preferred, and what is the main differences?

In what way are variables shared/inherited in each approach?

Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27
  • Your last point is a separate problem, ask about it somewhere else. But search first, there ore other questions about this here. – Vladimir F Героям слава Oct 01 '16 at 09:49
  • I removed that last question. And yes, the answers in the "duplicate" were helpful! But I didn't find it with the key-words I used. – Jonatan Öström Oct 01 '16 at 10:22
  • Some level of duplication is good, because it helps people to find their answers in a question with different wording, so don't worry. The point is it is pointless to just copy answers from the other question here. – Vladimir F Героям слава Oct 01 '16 at 10:26
  • Try to have a single question in your post (see the *too broad* closing). The inheriting could also be a separate question. When it comes to access to variables, it happens with *host association*. Anything that is in the `contains` section of something has access to the variables of the host. Module procedures have access to the module variables and internal procedures have access to the host program or host procedure. – Vladimir F Героям слава Oct 01 '16 at 10:28

1 Answers1

2

An interface block is extra work. You have to write the block and revise it when the procedure changes. Interface blocks are useful for purposes such as using the ISO C binding facilities to call a C language routine.

Having a subroutine under "contains" in the main program has the disadvantage that all local variables are inherited. I think that is better for procedures to be more isolated.

Modules make the procedures more isolated, with the access of variables clearer -- unless you make excessive use of module variables. Additionally, its easier to reuse procedures from modules in other programs.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • So subroutines after a `contains` statement do not inherit variables from each other? And i could safely use overlapping sets of variable names in subroutines in the same `contains` block, whether it is in the main code or in a module? Or does the callee inherit from a calling subroutine, for example? – Jonatan Öström Oct 01 '16 at 09:57
  • If you have to answer basically the same words as in the answer it is an indication it probably is a duplicate. – Vladimir F Героям слава Oct 01 '16 at 09:59