Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Characters as function names

erhanturan
Beginner
768 Views
Hello,

InFortran, we can use functions as arguments of procedures. However, while doing that we supply the names of the functions by ourselves. Assume that I have two characters myF1 and myF2 with values 'F1' and 'F2', respectively.

If a had an external function named as F1, then

some_other_function(F1)

might work but what if I want something like

some_other_function(myF1)

where the name is a character, this would give a run-time error. But my point is, I want to decide on the argument depending on the computed values comes from somewhere else on the program. If I calculate 3 let's say, then I want to call F3, if 5 then F5. This is just to generalization of an existing code. Of course one can always add select case statements of define function names upto F99 for instance.

This might look unnecessary but it is extremely useful. In Matlab forinstance, something similar can be perfomed. I wonder if such an operation is possible with Intel Fortran or maybe with Fortran 2003 extensions.


Erhan

0 Kudos
4 Replies
ZlamalJakub
New Contributor III
768 Views
Try something like this (it works on dlls where function given by myF1 must be declared as DLLEXPORT, but I do not know if it will work on procedure which is in your exe file as I suppose it is).

pProc=GetProcAddress("ourExe.exe"C,trim(myF1)//""C)

some_other_function(pProc)


recursive subroutine some_other_function(sub)
external sub
call sub()
end subroutine


I hope it helps

Jakub

0 Kudos
erhanturan
Beginner
768 Views
Thanks for the reply but this might not help. I said external functions but they are actually not external, functions are defined in the code indeed. Since I'm also using linux as well, I need something portable.

By the way, all of my functions are in a module so they are accessible with "use module" instead of defining them as
real, external - one by one.
0 Kudos
jimdempseyatthecove
Honored Contributor III
768 Views

Erhan,

Create an array of function addresses

some_other_function(myF(1))

Jim Dempsey
0 Kudos
jimdempseyatthecove
Honored Contributor III
768 Views

If you are concerned about possible argument errors in release mode then make myF(n) a function that returns the function address (or reference to the address), and performs sanity check in the process.
0 Kudos
Reply