Question

Well, this is the issue I've today...

I'm writing a module procedure that has, as an argument, a function. This module looks something like this:

module Integ
    implicit none
     <variables declaration>
contains
    function Integral(a,b,f) result(res)
        real, intent(in)     ::a, b
        real                 ::res

        interface
            pure function f(x)
                real, intent(in) :: x
                real             :: f
            endfunction
        endinterface


     <more code of function Integral>

    endfunction Integral

endmodule Integ

Well, up to here, everything is great. The issue appears when I try to use this function with a Fortran intrinsic function. I.e., in this code:

program main

use Integ

implicit none

real   ::res,a,b

a=3.0; b=4.0

res=Integral(a,b,sin)  !<- This line does not work

!res=Integral(a,b,sen) !<- This line does work

contains
    function sen(x)
        real, intent(in)   :: x
        real               :: sen

        sen=sin(x)
    endfunction

endprogram

The first line does not work, giving the error message:

main.f90(17): error #6404: This name does not have a type, and must have an explicit type.   [SIN]
r=Int1DMonteCarlo(0.0,1.0,sin,10000)
--------------------------^

main.f90(17): error #6637: This actual argument must be the name of an external user function or the name of an intrinsic function.   [SIN]
r=Int1DMonteCarlo(0.0,1.0,sin,10000)
--------------------------^

But the second line (comented in the snipplet) does.

Those errors are quite disorienting for me, because sin is a Fortran intrinsic function (thing that contradicts error nr 2), and in consequence explicit in every scope (thing that contradicts the error nr 1).

Obviously I'm doing something wrong but I don't know what.

So I would like to ask:

  • It is possible to call a module procedure with an intrinsic function as an actual argument?
  • I'm loosing something aside from declaring the interface inside the procedure?

If you are interested this is the complete source of the module and this is the source of the main

Sorry if I'm asking a stupid question. I think I am doing things the way the books I'm reading now (Metcalf, Numerical recipes for Fortran V:II) are telling me.

Thank you for your time!

Was it helpful?

Solution

Use an intrinsic statement in the main program to declare that the actual argument sin is the intrinsic. This requirement is spelled out in the description of the intrinsic attribute in the Fortran standard.

With an eye to the future, you may be better off writing your own wrapper function around the intrinsic - create a function mysin that simply calls sin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top