Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28701 Discussions

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

Simon1
Beginner
639 Views

Hello,

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()

contains

function func()
real(8) :: func
func=0.d0
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

Simon.

0 Kudos
1 Solution
Steven_L_Intel1
Employee
639 Views

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.

View solution in original post

0 Kudos
5 Replies
Steven_L_Intel1
Employee
640 Views

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.

0 Kudos
Simon1
Beginner
639 Views

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.

0 Kudos
Steven_L_Intel1
Employee
639 Views

Technically, that is also not allowed.

0 Kudos
jimdempseyatthecove
Honored Contributor III
639 Views

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

Jim Dempsey

0 Kudos
Simon1
Beginner
639 Views

Hello Jim.

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

0 Kudos
Reply