我不知道如果這是你在找什麼,但:
的Fortran,您還可以通過周圍的子程序/功能名稱作爲實際參數子程序/函數調用。相應的僞參數必須具有「外部」屬性。
subroutine fobj(n,x,f,func)
implicit none
integer :: n
real(8),external :: func
real(8) :: f
real(8) :: x(n)
intent(in) :: n,x
intent(out) :: f
!OBJECTIVE FUNCTION
f=func(x,n)
end subroutine fobj
function func1(x,n)
implicit none
real(8) func1
integer n
real(8) :: f,x(n)
f = x(1)**2-x(2)+2*x(3)
end function func1
function func2(x,n)
implicit none
real(8) func2
integer n
real(8) :: f,x(n)
f = x(1)**2+x(2)+2*x(3)
end function func2
program main
real(8),external :: func1,func2
real(8),allocatable :: x(:)
real(8) :: f
integer n
n=50
allocate(x(n))
x=10. !Set X to a known value
call fobj(n,x,f,func1) !Call func1
print*,f !10**2-10+2*10 = 110
x=10. !Reset X ... just to make sure there is no funny business in func1,func2
call fobj(n,x,f,func2) !Call func2
print*,f !10**2+10+2*10 = 130
deallocate(x)
end program main
當然,這個程序做無非呼叫FUNC1和FUNC2在晦澀的方式有用其他,但希望它說明了這一點。如果您正在尋找在編譯時轉出功能,那麼我認爲一個include "myfile"
可能是清潔劑(只是切換哪個文件你包括在時間@AlejandroLL的建議)
不太確定,你想從哪裏撥打電話? – haraldkl 2012-03-09 22:58:10
不太確定:你想在編譯之前或之後修改函數嗎?在Fortran中,編譯後無法修改源文件。 – max 2012-03-11 11:25:02