Reputation: 301
I have a fortran library to which I must pass a function with a very specific format. The library then is doing some operation on my function. The function is written by a user (like me) and the library is given for granted.
Unfortunately to compute my function I need some values (some of them could be initialized once and for all in the main) and I would like to avoid the use of common
or save
.
I read I could use a singleton pattern but I am not very expert in template and on top of that some people criticize its use. So, how can I pass my variable inside the function even if I cannot have it in the arguments of my function?
Upvotes: 2
Views: 2329
Reputation: 3264
If you define a Fortran MODULE
, you can have your function use some variables not defined in the main program:
MODULE mymod
REAL :: x = 1.35
INTEGER :: y = 16
CONTAINS
FUNCTION results(a,b)
REAL :: a, results
INTEGER :: b
results = a*x+real(b+y)
END FUNCTION results
END MODULE mymod
PROGRAM extrn_func
USE mymod, ONLY: results
PRINT *,results(1.0, 3)
END PROGRAM extrn_func
Though, this method requires you to be able to add the USE mymod
statement to the main program.
Upvotes: 4