これはあなたが探しているものであれば、私はわからないんだけど、:
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
コンパイル前またはコンパイル後に関数を変更したいですか? Fortranでは、コンパイル後にソースファイルを変更することはできません。 – max