davis@pacific.mps.ohio-state.edu ("John E. Davis") (10/11/90)
Hi, What is the easiest way that I can define a variable that points to a function in fortran? That is, I want to have a variable that contains the address of some function subprogram and I want to be able to change this varaible to point to other subprograms. Why do I want to do this? I am writing a routine that does integrals of the form g(x) = f(x) * a_messy_expression(x). So I have a subprogram such as: real*8 function do_integral(f,... other parameters) external f . . C! C! Now, In this subprogram, I call some standard library routines to do parts C! of the integral and the standard library routines need to be called as in: C! . . call std_lib_routine(g,....other parameters....) . . where the standard library needs g to be a function of the single variable x. However, my g(x) also depends on another function f(x). If I had only one function f(x), then there would be no problem. However I have several functions f(x). These easiest thing would be to define f to be a pointer to a function and pass the pointer value through a common block to the function subprogram for g(x). Then whenever the standard library evaluates g(x), the f in g(x) will be pointing at the correct f(x). Is this clear? All this could be avoided if the standard library routine allowed f to be passed as well as g as in: real*8 function do_integral(f,...) external f,g . . call std_lib_routine(g,f,...) . . end subroutine std_lib_routine(g,f,...) external g,f . . g_of_x = g(f,x) . . end real*8 function g(f,x) external f . . g = f(x) * messy_expression(x) end But the standard library does not operate this way. Of course I can always duplicate code but and reanme things but this is not very elegant. What do fortran programmers do in a situation like this? Thanks, -- John bitnet: davis@ohstpy internet: davis@pacific.mps.ohio-state.edu
3003jalp@ucsbuxa.ucsb.edu (Applied Magnetics) (10/12/90)
In article <DAVIS.90Oct11025317@pacific.mps.ohio-state.edu> davis@pacific.mps.ohio-state.edu ("John E. Davis") writes: > What is the easiest way that I can define a variable that points to a >function in fortran? I wish. You can do two things with a subprogram: 1) call it; 2) declare it EXTERNAL and pass it to another subprogram. No assignments, unless you write a primitive in another language and are willing to port it when the time comes. BTW, some machines need more than one word to hold a function pointer. The only all-Fortran solution I can think of is to set up a dispatcher routine: real function F(x) real x integer opcode common/hidden/opcode goto (1,2,3...), opcode 1 F= F1(x) return 2 F= F2(x) return C ... end You use the opcode as a substitute for function pointers. Sigh. --P. Asselin