Here is a typical usage of function pointers in C. I would like to do something similar in Fortran. I have some ideas, but I would like to know if there is some canonical way to do so.
The function pointers and contexts passed in by the user are stored, then called later.
typedef PetscErrorCode (*TSIFunction)(TS,PetscReal,Vec,Vec,Vec,void*); PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx);
The user's function is called back using their context at various later times.
In PETSc, they also make heavy use of string -> function pointer tables. Everything is a plugin, so the user can register their own implementations and they are first-class.
#define PCGAMG "gamg" PCRegisterDynamic(PCGAMG ,path,"PCCreate_GAMG",PCCreate_GAMG);
This registers the creation routine in an "FList", then PCSetFromOptions() offers the ability to choose this method versus any of the other choices. If the system supports dynamic loading, you can skip the compile-time dependency on the PCCreate_GAMG symbol and just pass NULL, then the symbol will be looked up in the shared library at run time.
Note that this one step beyond a "factory", it's an inversion of control device similar to what Martin Fowler calls "service locator".
Note: this came up in my private correspondence with Jed Brown, where he asked this question to me. I decided to outsource it and see what answers people can come up with.