c=========================================================== c Demonstration main program and subprograms c illustrating the 'EXTERNAL' statement and how c subprograms may be passed as ARGUMENTS to other c subprograms. This technique is often used to c pass "user-defined" functions to routines which c can do generic things with such functions (such c as integrating or differentiating them, for example). c=========================================================== program texternal c----------------------------------------------------------- c The 'external' statement tells the compiler that the c specified names are names of externally-defined c subprograms (i.e. subroutines or functions) c----------------------------------------------------------- real*8 r8fcn external r8fcn, r8sub2 c----------------------------------------------------------- c Call 'r8fcncaller' which then invokes 'r8fcn' c----------------------------------------------------------- call r8fcncaller(r8fcn) c----------------------------------------------------------- c Call 'r8subcaller' which then invokes 'r8sub2' c----------------------------------------------------------- call subcaller(r8sub2) stop end c=========================================================== c Input 'fcn' is the name of an externally defined c real*8 function. This routine invokes that function c with argument 10.0d0 and writes the result on c standard error c=========================================================== subroutine r8fcncaller(fcn) implicit none real*8 fcn external fcn real*8 fcnval fcnval = fcn(10.0d0) write(0,*) 'r8caller: ', fcnval return end c=========================================================== c Input 'sub' is the name of an externally defined c subroutine. This routine invokes that subroutine c with arguments 10.0d0 and 20.0d0. c=========================================================== subroutine subcaller(sub) implicit none external sub call sub(10.0d0,20.0d0) return end c----------------------------------------------------------- c Demonstration real*8 function c----------------------------------------------------------- real*8 function r8fcn(x) implicit none real*8 x r8fcn = x**2 return end c=========================================================== c Demonstration subroutine c=========================================================== subroutine r8sub2(x,y) implicit none real*8 x, y write(0,*) 'r8sub: x = ', x, ' y = ', y return end