1

Recently, I started using the gfortan-10.2-BigSur-Intel compiler and observed ambiguous behavior when a function defined in a module is to write a string to standard output. Concretely, I have a module function "PrintToScreen" that prints a given character string "str" to standard output, while returning an integer error code. Then, I ask to write the error code in the calling program, using the code

INTEGER (KIND = 4) :: ios

...

ios = PrintToScreen(str);
PRINT '(t1, a, i4)', 'ios = ', ios

...

As a result, "str" is displayed on screen by "PrintToScreen", followed by ios, as expected. However, if I avoid the detour via "ios" by using the simpler code

...

PRINT '(t1, a, i4)', 'ios = ', PrintToScreen(txt)

...

the program stalls as soon as the function "PrintToScreen" is entered, while also "str" is left unprinted. Also, if I make "PrintToScreen" write "str" to a text file instead of standard output, the two pieces of code yield the same result. Finally, I checked the code on a Linux machine (Debian Buster) and no problems occurred.

How do I fix this?

francescalus
  • 30,576
  • 16
  • 61
  • 96
WCJM
  • 11
  • 1
  • 1
    As the linked question says, you have broken code when a function in an output statement itself outputs to the same unit. The two approaches you've seen which work (taking the function reference out of the output statement; having the function and the outer output write to different units) are appropriate. – francescalus Feb 08 '21 at 17:05
  • I acknowledge the problem of the same unit being addressed simultaneously and this explanation is certainly helpful to understand similar problems - thanks for that - , but the sad thing is that this practice is tolerated silently on other platforms. – WCJM Feb 08 '21 at 18:14
  • There are compilers which can be more clear in reporting coding errors like this one. However, this is an area of Fortran where the compiler isn't obliged to help you find out what the problem is, so it comes down to the priorities of the compiler writer and user. – francescalus Feb 08 '21 at 19:43

0 Answers0