I would like to write a Fortran function that takes another function as input. I know that I can do this via an interface. From now on I will call these two different functions the 'function' and the 'interfaced function'.
Imagine that my function only varies the first argument of the interfaced function. For example, it might be an integration routine that is integrating in x
, which is the first argument of any interfaced function that I would pass to it.
I would like to be able to pass an interfaced function with any number of arguments to this first function, and somehow be able to set fixed constant values for any other arguments that the interfaced function requires. A minimal example of the type of thing I want is below:
PROGRAM test
IMPLICIT NONE
!This works
WRITE(*,*) add(g)
!This does not work
WRITE(*,*) add(h(:,0.))
CONTAINS
REAL FUNCTION add(f)
IMPLICIT NONE
INTERFACE
REAL FUNCTION f(x)
REAL, INTENT(IN) :: x
END FUNCTION f
END INTERFACE
add=f(1.)+f(2.)
END FUNCTION add
REAL FUNCTION g(x)
IMPLICIT NONE
REAL, INTENT(IN) :: x
g=x**2
END FUNCTION g
REAL FUNCTION h(x,y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
h=x**2+y**2
END FUNCTION h
END PROGRAM test
The above code will not compile with the second add
call in the main program and complains of a syntax error in the argument list. However, it works fine without this line.
Is there any way I can adjust my code so that I can call my add
function with the input h
function as well as with the input g
function?
Note that the actual application I have in mind for this is relatively complex, and involves 'interfaced functions' that would need all sorts of different types in their argument list.