If printing occurs within function f
, the behavior of print*,f()
is undefined, since recursive I/O is not allowed in Fortran, and instead you should write
y = f()
print*,y
See the previous thread https://fortran-lang.discourse.group/t/gfortran-bug-on-a-simple-program.