I am trying to pass the name of a subroutine as an argument to a function, but cannot work out how to avoid a compilation error. I can pass a function as an optional argument, but not a subroutine. In the following code, module m0 defines a function and a subroutine. Modules m1 and m2 then define functions that take an optional function and subroutine, respectively, as arguments. The compiler complains at the definition of s as external, but if s is not optional (and the test for whether s is present is removed) then the program compiles.
Any assistance on what I need to do to get this to work would be greatly appreciated.
MODULE m0
CONTAINS
FUNCTION f ()
INTEGER :: f
f=1
RETURN
END FUNCTION f
SUBROUTINE s ()
CONTINUE
RETURN
END SUBROUTINE s
END MODULE m0
MODULE m1
CONTAINS
FUNCTION f1(f)
INTEGER, EXTERNAL, OPTIONAL :: f
IF (PRESENT(f)) THEN
f1=f()
ELSE
f1=0
END IF
RETURN
END FUNCTION f1
END MODULE m1
MODULE m2
CONTAINS
FUNCTION f2(s)
EXTERNAL, OPTIONAL :: s
IF (PRESENT(s)) THEN
CALL s ()
f2=1
ELSE
f2=0
END IF
RETURN
END FUNCTION f2
END MODULE m2
PROGRAM p
USE m0
USE m1
USE m2
i=f1(f)
i=f2(s)
END PROGRAM p