write(*,*) in function called by another write(*,*)

write(*,*) in function called by another write(*,*)


I used to put some "write(*,*)" in my code when I want quick and easy checks of what is happening.

Today I encountered an error while doing this with ifort :

Here the sample program :

program test
implicit none

write(*,*) func(),func()


function func()
real(8) :: func
write(*,*) "Inside func"
end function func

And the output:
 forrtl: severe (40): recursive I/O operation, unit -1, file unknown

I guess, someway the compiler get lost with a write to * inside a write to *.
Indeed if I change write(*,*) "Inside func" to write(6,*) "Inside func" (6 being preconnected to the default output), the error disappears.

Changing to

write(*,*) func()
write(*,*) func()

in the main part is ok too.

Am I missing something in write mechanism that should forbide me to use it this way ?

Gfortran compile and execute without error the code.
Ifort version 14.0.2


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

The standard does not allow you to start an I/O operation on a unit while another operation is already in progress. In ifort, by default, unit * and unit 6 are not the same unit. gfortran apparently has an extension to allow you to do this, but it is an extension.

Retired 12/31/2016

Ok, thanks for the answer, I didn't know that but It makes sense.

I suppose that even if the second change (only one write(*,*) func() per line) works fine, it is incorrect and should not be used.

Technically, that is also not allowed.

Retired 12/31/2016

For tracing purposes, you could call the appropriate C runtime library routine directly (after formatting to "internal file"/text variable).

Jim Dempsey

Hello Jim.

I'm not certain to well understand your suggestion.
Can you develop a little more ?

Leave a Comment

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