【发布时间】:2021-03-03 17:46:32
【问题描述】:
我试图通过使用指针将一个函数作为另一个函数的参数。我不断收到错误消息:Error: Invalid procedure argument at (1)。关于主程序中 diff 中的参数 f_ptr。有谁知道如何解决这个问题?
module functions
contains
function f(x) result(y)
double precision x, y
y = sin(x)
end function f
end module
MODULE numerical_math
contains
function diff(f, x, degree) result(df)
double precision, intent(in) :: x
double precision :: h, df
integer :: degree, i, j
integer, allocatable, dimension(:) :: lst
pointer :: f
interface
function f(x) result(y)
double precision :: x, y
end function f
end interface
...
end function diff
END MODULE numerical_math
program main
use numerical_math
use functions
implicit none
integer :: first
procedure (f), pointer :: f_ptr => null ()
f_ptr => f
print *, f_ptr(1.d0)
do first = 1,9
print *, diff(f_ptr, 0.d0, first)
end do
end program main
【问题讨论】:
-
在传递函数时根本不需要函数指针。函数指针仅在 Fortran 2003 中添加用于更高级的东西,例如存储函数地址并在以后更改它以供其他使用。你只需要普通的函数参数。
-
@SamWoef 您使用的是哪个编译器和版本?
-
我在 netbeans IDE 上使用 mingw32-gcc-fortran
-
如果您在
main和diff中调用diff(f, 0.d0, first)会发生什么情况?删除pointer行? -
还是同样的错误。说清楚,我离开界面吧?
标签: pointers fortran procedure