This code hangs due to the "whatever" printing statement inside the function, when called as an argument to print. Wild guess is that it tries to get some lock during the Squared print, which is already taken by the caller, but it's pure speculation. Tested on ifort, works as expected (no hang, two prints). module Moo contains subroutine main() print *, Squared(5) end subroutine function Squared(v) integer, intent(in) :: v integer :: Squared Squared = v*v print *, "whatever" end function end module program foo use Moo call main() end program
Your code is invalid, because it is doing trying to do recursive IO. From the Fortran 2003 standard: An input/output statement that is executed while another input/output statement is being executed is called a recursive input/output statement. A recursive input/output statement shall not identify an external unit except that a child data transfer statement may identify its parent data transfer statement external unit. A data transfer statement that includes a derived-type list item and that causes a user-defined derived-type input/output procedure to be invoked is called a parent data transfer statement. A data transfer statement that is executed while a parent data transfer statement is being processed and that specifies the unit passed into a user-defined derived-type input/output procedure is called a child data transfer statement. You are invoking undefined behavior, and are clearly getting an acceptable result.
Aren't you using some Mac OSX? If so, it is a duplicate of pr30617.
> Aren't you using some Mac OSX? If so, it is a duplicate of pr30617. No answer, so closing as duplicate. *** This bug has been marked as a duplicate of bug 30617 ***