1

次のようなコードがあります

subroutine sub1(f)
    interface
        function f(x)
            (description of f)
        end function f
    end interface

    (do something with f)
end subroutine sub1

subroutine sub2(f)
    interface
        function f(x)
            (description of f)
        end function f
    end interface

    (do something with f)
end subroutine sub2

ただし、2 つのサブルーチンsub1sub2両方は、ダミー関数に対して同一のインターフェイスを使用しますf。これら 2 つのプロシージャが同じインターフェイスを共有するようにするにはどうすればよいですか (たとえば、モジュールを使用して)? プロシージャ ポインタを使用する必要がありますか?

4

1 に答える 1

3

abstract interfaceモジュール内の sなどの再利用可能な「関数型」を定義できます。

module m
    implicit none
    abstract interface
        function der(x,y) result(yDot)
            real, intent(in) :: x, y
            real :: yDot
        end function
    end interface
end module

subroutine integrateEuler(derY,x0,xf,y)
    use m
    real, intent(in) :: x0, xf
    real, intent(inout) :: y
    procedure(der) :: derY
    ! code here
end subroutine

subroutine integrateRKF45(derY,x0,xf,y)
    use m
    real, intent(in) :: x0, xf
    real, intent(inout) :: y
    procedure(der) :: derY
    ! code here
end subroutine

関数ポインタを使用する必要はありませんが、同じ方法で宣言できます。procedure(der), pointer :: funPtr => myFun

于 2016-01-21T23:14:26.137 に答える