Format label and Contains block

Format label and Contains block

Hi all,

i'm working on a main routine with many other subroutine in a contains block. In the main one, I defined some format labels but I can't use them in the contains block : they are no more defined. At the moment, I avoid this problem by redefined format labels in each subroutine in the contains block ; but this solution isn't really suitable. Is there a way to define these labels only once ?



6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Rather than format statements, consider using format specifications in character constants.

  CHARACTER(*), PARAMETER :: my_format = "('Here is an example format specification.')"
  PRINT my_format

The advantage of character constants is that their name can be more descriptive, plus, as they are just "normal" parameters, they can be provided in a module or in a host scope (your situation) or whatever.

Very clever, Ian.  In your syntax, it seems that you package the format specification within a "variable".  In that sense, it could be even placed within a Fortran module, which can be accessed by some of the program units Sebastian is using which use this format.  In this way, it might could be defined once and applied to all units in which it's needed via a Use statement.

>> it seems that you package the format specification within a "variable".

While the sample snip used PARAMETER you could leave it as a variable. Then at run time you could load up the variable with the desired contents.(you initialize it with default values at compile time)

Jim Dempsey

A terminology correction - you have contained procedures, not a contained "block". While variables in the parent procedure are visible in the contained procedure, statement labels (and formats) are not. Ian's suggestion of a parameter constant for the format is a good one - there is no performance penalty for that. Just make sure you use PARAMETER so the compiler can see it at compile time.

Retired 12/31/2016

Thanks for all your answers, that's exactly what I need. I just try it, and it works very fine.


Leave a Comment

Please sign in to add a comment. Not a member? Join today