1

我正在做一个项目(明天到期:/),当我尝试在我自己编写的函数上使用单纯形算法时遇到了一个问题。它没有用,经过 5 个小时的搜索和实验,我发现了以下内容:

当我将函数传递给子例程时,该函数不能有任何数组参数。这是真的?因为在我应该使用它的代码中显然应该工作。

我正在使用 ifort 编译器。请参阅下面的最小示例,我基本上取自http://malayamaarutham.blogspot.de/2006/02/passing-function-names-as-arguments-in.html

  ! Author : Kamaraju S Kusumanchi
  ! Email  : kamaraju@gmail.com
  ! Last edited : Sun Feb  5 2006
  !
  ! Sample program demonstrating the use of external attribute.        This program
  ! shows how to pass function names as arguments in Fortran 90       programs.
  !
  ! Compilation and execution steps
  ! $gfortran passing_functions.f90 -o passing_functions
  ! $./passing_functions
  !  beta =    5.500000
  !  beta =    1.500000
  !
  ! I would appreciate any comments, feedback, criticism,       mistakes, errors etc.,
  !   (however minor they are)
  !
  module dummy
    implicit none
  contains
  !------------------------------------------------------------------------------
  function func1(a)
    implicit none
    real :: a
    real :: func1

    func1 = a+5
  end function func1
  !------------------------------------------------------------------------------
  function func2(b)
    implicit none
    real :: b(:)
    real :: func2

    func2 = b(1)
  end function func2
  !------------------------------------------------------------------------------
  function func3(dyn_func, c)
    implicit none
    real :: c
    real, external :: dyn_func
    real :: func3

    func3 = dyn_func(c)
  end function func3
  !------------------------------------------------------------------------------
  function func4(dyn_func, c)
    implicit none
    real :: c(*)
    real, external :: dyn_func
    real :: func4

    func4 = dyn_func(c)
  end function func4
  end module dummy
  !------------------------------------------------------------------------------

  program passing_functions
    use dummy
    implicit none

    real :: alpha=0.5, beta
    real :: gamma(2) = (/10,20/)

    beta = func3(func1, alpha)
    write(*,*) 'beta = ', beta
    beta = func4(func2, gamma)
    write(*,*) 'beta = ', beta
  end program passing_functions

这是输出:

zeus$ passing.out
 beta =    5.500000    
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
passing.out        0000000000402D44  Unknown               Unknown  Unknown
passing.out        0000000000402C7C  Unknown               Unknown  Unknown
libc.so.6          00002AFF7915D23D  Unknown               Unknown  Unknown
passing.out        0000000000402B79  Unknown               Unknown  Unknown
zeus$ 
4

1 回答 1

5

另一个答案可用于解决您的问题,但有更简单的方法而不使用指针。

几乎没有理由external在现代 Fortran 中使用。在我在 Stackoverflow 上看到的 Fortran 问题中,它几乎总是将人们引向错误的方法。除非你真的确定,否则不要使用external.

您要做的是在函数func3func4withinterface块中声明(即描述)函数参数。以下是如何做到这一点func4......你应该能够弄清楚func3

 function func4(dyn_func, c)
    implicit none
    real, dimension (:) :: c
    interface
       function dyn_func (x)
          real :: dyn_func
          real, dimension (:) :: x
       end function dyn_func
    end interface
    real :: func4

    func4 = dyn_func(c)
  end function func4
于 2013-03-14T00:59:14.590 回答