Reputation: 13
I have a variable that stores the name of of a subroutine. Is there a way I can call the subroutine by using "call [variable]".
I have several subroutine (example names: X_1, X_2, X_3, etc), and the user provides the number (1, 2, 3, etc), and then the code is supposed to operate subroutine associated with the number.
Upvotes: 1
Views: 248
Reputation: 60008
This is not possible automatically by the compiler. You must prepare a table which stores the numbers or names according which they should be selected and then call the right subroutine.
After that you can choose the right function using a select case construct.
select case (n)
case (1)
call subroutine_1
case (2)
call subroutine_2
end select
or
select case (name)
case ("subroutine_1")
call subroutine_1
case ("subroutine_2")
call subroutine_2
end select
You can also use a table with function pointers. The amount of work required will be similar.
Upvotes: 4