pointers - How to use inteface blocks to pass a function to a subroutine? -
i understand interface command can used pass a function subroutine. example in main program i'd define function , pass subroutine like:
mainprogran use .... implicit none type decorations etc interface function test(x,y) real, intent(in) :: x, y real :: test end function end interface call subroutine( limit1, limit2, test, ans) end mainprogram is correct way of doing this? i'm quite stuck! within subroutine there need put let know function coming in? subroutine in case library don't want have keep recompiling change function.
module:
module fmod interface function f_interf(x,y) real, intent(in) :: x, y real :: f_interf end function end interface contains function f_sum(x,y) real, intent(in) :: x, y real f_sum f_sum = x + y end function function f_subst(x,y) real, intent(in) :: x, y real f_subst f_subst = x - y end function subroutine subr(limit1, limit2, func, ans) real limit1, limit2 procedure(f_interf) func real ans ans = func(limit1, limit2) end subroutine end module main program:
program pass_func use fmod implicit none real ans, limit1, limit2 limit1 = 1.0 limit2 = 2.0 call subr( limit1, limit2, f_subst, ans) write(*,*) ans call subr( limit1, limit2, f_sum, ans) write(*,*) ans end program pass_func and output:
-1.000000 3.000000
Comments
Post a Comment