Closed koshMer closed 3 years ago
This is an issue because your functions don't "know" about each other. This is a quirk of Fortran, where unlike C it does not automatically know to reference a function declared in the same flat file when you make the call to add
from within triple_add
. There are three ways to solve this problem.
1) Encapsulate both functions within a module, so that they implicitly have access to each other's interfaces.
2) Explicitly define an interface for the function add
within the function triple_add
.
3) If you don't need external access to add
then you can place it inside of triple_add
after a contains
keyword.
I think (1) and (3) are self explanatory, but here's an example of what option (2) should look like:
pure function add(val1, val2) result(val3)
use iso_fortran_env, only: real32
implicit none
real(real32), intent(in) :: val1,val2
real(real32) :: val3
val3 = val1+val2
end function add
pure function triple_add(val1,val2) result(val3)
use iso_fortran_env, only: real32
implicit none
real(real32), intent(in) :: val1,val2
real(real32) :: val3
interface
pure function add(val1, val2) result(val3)
use iso_fortran_env, only: real32
real(real32), intent(in) :: val1,val2
real(real32) :: val3
end function add
end interface
val3 = add(val1,val2)*3.0
end function triple_add
☝️ this code makes everything compile and run successfully for me.
If you go with option (1), then your calls in python will need to look like code.<module_name>.add
and code.<module_name>.triple_add
.
Sorry for any confusion, I just edited my original incorrect response. The above should solve your problem!
Oh, something I hadn't encountered yet. Up until now I always defined my functions inside modules. Thank you for the small lesson on Fortran! :)
With code.<module_name>.<function_name>
and using your suggestion 1) I can access all functions from python without any errors! :)
Hi, its me again :)
I tried to put several functions in one file
test2.f90
and use it in python. However, the compilation of this small program fails. The code in question:The python code i use to run it:
And the error output:
I am not really sure why I am getting any of those errors when compiling the fortran code. It seems to me that both functions are pure and i state the type for all variables and only use real32....
It works well if I use only the function
add(val1,val2)
. Do I have to structure programs differently? Should I define functions in a fortran module?