1

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?

1 Answer 1

3

There isn't really a way to write a Fortran statement such as

call character_variable_containing_subroutine_name

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, cos or tan.

First, capture the text of the argument to the program into a character variable:

character(len=*) :: user_choice
...
call get_command_argument(1,user_choice)
...
select case (user_choice)
case ('sin')
   ... do stuff with sin
case ('cos')
   ... do stuff with cos
case ('tan')
   ... do stuff with tan
case default
   ... do whatever
end select

You could make this more elaborate by using procedure pointers. For example, you might define:

pointer :: rp
interface
 real function rp(inval)
    real, intent(in) :: inval
  end function rp
end interface

and then replace the first version of the select case construct by something like:

select case (user_choice)
case ('sin')
   rp => sin
case ('cos')
   rp => cos
case ('tan')
   rp => tan
case default
   ... do whatever
end select

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your answer, I got what you meant. It is completely clear for me for intrisic functions, since I know all them, therefore it is possible make a choice based on the user input using a select case, as your example shows. But in my case, the user written a custom procedure and compiled it together with my program. Then I would like to know if the user can choose any name for this routine and pass this name for me as an argument (on a file, for example). As you answered, it is not possible. Thank you.
old, but this answer doesnt really answer question. You want dynamic share library reads. Look into dl_open, which is a c function but you can find fortran wrappers. This will let you query contents of a library and call functions you find there. If you make a library of functions with a standard interface or possible with a naming convention for the interface you can use this to dynamically call functions that aren't know or even didn't exist at compile time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.