I do not want to use common blocks in my program. My main program calls a subroutine which calls a function. The function needs variables from the subroutine.
What a
Below is an example of how you may achieve this...
The code has been adapted from a BFGS method to show how you can pass functions and call other functions within a module...
Here I use:
Hopefully this will cover everything for you...
Module Mod_Example
Private :: private_func
SUBROUTINE test_routine(res,start,fin,vector,func,dfunc)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: res, start, fin
REAL, DIMENSION(:), INTENT(INOUT) :: vector
INTERFACE
FUNCTION func(vector)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL :: func
END FUNCTION func
FUNCTION dfunc(vector)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL, DIMENSION(size(vector)) :: dfunc
END FUNCTION dfunc
END INTERFACE
! do stuff with p
private_func(res,start,fin,vector,func,dfunc)
! do stuff
END SUBROUTINE test_routine
SUBROUTINE private_func(res,start,fin,vector,func,dfunc)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: res, start, fin
REAL, DIMENSION(:), INTENT(INOUT) :: vector
INTERFACE
FUNCTION func(vector)
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL :: func
END FUNCTION func
FUNCTION dfunc(vector)
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL, DIMENSION(size(vector)) :: dfunc
END FUNCTION dfunc
END INTERFACE
! do stuff
END SUBROUTINE private_func
END Mod_Example
func and dfunc would be declared within the program code that uses the MODULE Mod_Example with an interface block at the top.res, start etc. can be declared with values in the main program block and passed to SUBROUTINE test_routine as arguments.SUBROUTINE test_routine will call private_func with the variables that were passed to it.Your main program would then look something like this:
Program Main_Program
USE Mod_Example
INTERFACE
FUNCTION func(vector)
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL :: func
END FUNCTION func
FUNCTION dfunc(vector)
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL, DIMENSION(size(vector)) :: dfunc
END FUNCTION dfunc
END INTERFACE
! do stuff
! calls test_routine form module
! uses dfunc and func defined below
call test_routine(res,start,fin,vector,func,dfunc)
! do stuff
END PROGRAM Main_Program
! define dfunc and nfunc for passing into the modular subroutine
FUNCTION func(vector)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL :: func
nfunc = vector
END FUNCTION func
FUNCTION dfunc(vector)
IMPLICIT NONE
REAL, DIMENSION(:), INTENT(IN) :: vector
REAL, DIMENSION(size(vector)) :: dfunc
dfunc = vector
END FUNCTION dfunc