Fortran:从另一个模块中的过程调用模块中的函数 [英] Fortran: Calling a function in a module from a procedure in another module
问题描述
程序Test_program
使用module_A
隐式无
整数:: i
i = 1
调用子程序_A(i)
结束程序Test_program
这个subroutine_A在模块A中,并依次调用module_B中的一个function_B:
module module_A
use module_B
implicit none
contains
子程序子程序_A(i)
隐式无
整型,意图(in):: i
双精度:: j
j =函数B(i)
结束子程序子程序_A
后端模块module_A
最后,module_B看起来像这样:
module module_B
隐式无
包含
双精度函数function_B(i)
隐式无
整数,意图(in):: i
function_B = 5.d0 * i
结束函数function_B
结束模块mo dule_B
程序和模块位于不同的文件中。不幸的是,这不会编译,因为我收到一条错误消息:
ERROR Subroutine_A:此子函数function_B的引用不在CALL语句中。
看来程序认为function_B是一个子程序,所以我不知道该怎么做。
顺便说一下,我正在尝试使用正确的封装我的子程序和函数使用模块,因为我被告知,但如果这不是正确的方式,我接受建议(我被告知不要使用接口,但模块,所以这个测试)。
谢谢 ,我实际上解决了这个谜团:我不是使用名称function_B,而是使用名称Compute_Error。当我将该函数名称更改为其他名称时,上述方法正常工作。看起来我链接到某个地方的某个库有一个子例程compute_error,虽然错误消息没有告诉我哪一个,或者如果那是肯定的问题。无论如何,再次抱歉,但我想我可以让这篇文章,因为它有助于看到如何链接模块和程序(我没有在互联网上找到这个特定示例的很多例子)。
当然这种使用模块和程序的方式不是正确的方式,随时添加一些有用的知识。
I admit the title might be a bit obscure, so let me give an example of what I want to do and what doesn't work. I have a main program which calls a subroutine which is in a module:
Program Test_program
Use module_A
Implicit none
Integer :: i
i = 1
call subroutine_A(i)
End program Test_program
this subroutine_A is in module A, and in turns calls a function_B which is in module_B:
module module_A
use module_B
implicit none
contains
subroutine subroutine_A(i)
implicit none
integer, intent(in) :: i
double precision :: j
j = function_B(i)
end subroutine subroutine_A
end module module_A
and finally, module_B looks like this:
module module_B
Implicit none
Contains
double precision function function_B(i)
implicit none
integer,intent(in) :: i
function_B = 5.d0*i
end function function_B
end module module_B
the program and modules are in different files. Unfortunately, this does not compile, as I get an error message:
ERROR Subroutine_A: This reference to subroutine function_B is not in a CALL statement.
It seems the program believes that function_B is a subroutine, so I am not sure what to do. By the way, I am trying to use proper encapsulation of my subroutines and functions using modules as I was told to, but if this is not the proper way I am open to suggestions (I was told not to use interfaces but modules instead hence this test).
Thanks
My apologies, I actually "solved" the mystery: instead of using the name function_B, I was using the name Compute_Error. When I changed that function name to something else, the method above worked. It seems some library I link to somewhere has a subroutine compute_error, though the the error message did not tell me which one, or if that was the issue for sure. Anyway, sorry again but I guess I can let the post since it helps to see how to link modules and procedures (I did not find many examples of that particular example on the internet). Of course of this way of using modules and procedures is not the proper way, feel free to add some useful knowledge.
这篇关于Fortran:从另一个模块中的过程调用模块中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!