Suppose I have a program in Fortran with various subroutines, I don’t know a priori all the subroutines, and an user supplies the name of one of them via command-line, just as follows:
program subroutine_name
Therefore, I store the subroutine_name in a character variable. In this way, I can’t declare a external variable to store the subroutine. So, how can I call it just knowing its name? Is it possible by this way, or is there another way to accomplish this?
There isn’t really a way to write a Fortran statement such as
It goes against the grain of statically-typed compiled languages such as Fortran to provide that sort of facility.
Of course, if you had asked can I provide an input argument to a Fortran program which will, at run-time, determine the execution path the program takes then the answer is of course. I’ll ignore any complications of your situation and suppose that you want to call one of
sin,cosortan.First, capture the text of the argument to the program into a character variable:
You could make this more elaborate by using procedure pointers. For example, you might define:
and then replace the first version of the
select caseconstruct by something like:This might simplify later code. I guess it might also make it more complicated.
Note that I haven’t tested any of these fragments, my syntax might be a little wonky.