I am trying to write a Fortran 77 program where a subroutine makes a function call, with the function being provided as an argument (to the subroutine).
The problem I am facing is that function 'bar' doesn't return the correct result. Here is a minimal (not) working example:
% cat mwe.f
real*8 function bar()
print *,"bar:",bar
bar = 101.0d00
print *,"bar:",bar
end
subroutine foo(func)
real*8 rv
rv = func()
print *,"rv:",rv
end
program tsig
external bar
call foo(bar)
end
% gfortran mwe.f && ./a.out
bar: 0.0000000000000000
bar: 101.00000000000000
rv: 0.0000000000000000
%
baris implicitly typed in the program andfoo) correspond to a declarationreal*8?real*8is the return type ofbar(as declared in that function statement). However, in the main program and infoothere is no such declaration. So,funcinfoois implicitly typed as default real, which may not be the same thing for you as (non-standard)real*8. Just becauservis declared asreal*8doesn't mean that the function resultfunc()is that.