I can create function pointers in Fortran 90, with code like
real, external :: f
and then use f as an argument to another function/subroutine. But what if I want an array of function pointers? In C I would just do
double (*f[])(int);
to create an array of functions returning double and taking an integer argument. I tried the most obvious,
real, external, dimension(3) :: f
but gfortran doesn’t let me mix EXTERNAL and DIMENSION. Is there any way to do what I want? (The context for this is a program for solving a system of differential equations, so I could input the equations without having a million parameters in my subroutines.)
The declaration “real, external :: f” doesn’t really make “f” into a full pointer since you can’t change the procedure that it points — it does permit you to pass this single function to another routine., So you also need the “pointer” attribute. There are examples on page 267 of “Fortran 95/2003 explained” by Metcalf, Reid & Cohen — a google search for “fortran procedure pointer” will bring up this page. A simple example close to yours is “real, external, pointer :: f_ptr”. Alternatively: “procedure (f), pointer :: f_ptr”. This is a Fortran 2003 feature — http://gcc.gnu.org/wiki/Fortran2003 and http://gcc.gnu.org/wiki/ProcedurePointers lists partial support in gfortran, best with 4.5. I’m not sure whether “dimension” is directly allowed, but you can assign a procedure to a pointer, which provides a lot of flexibility. You can also put the pointer into a derived type, which could be made into an array.
Edit: here is a code example which works with gfortran 4.5:
Edit 2: line commented out per comments below.