gpt4 book ai didi

linux - 从模块调用函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:39 25 4
gpt4 key购买 nike

我正在尝试做一些非常简单的事情,只需调用一个在模块中定义的函数,但它不工作,我不明白为什么。

这就是我使用 linux 编译它所做的

gfortran -o testingMOD testMod.f90 doubleMod.f90

这是错误

 testMod.f90:3.4:

use doubleMod
1
testMod.f90:8.15:

call double(n)
2
Error: 'double' at (1) has a type, which is not consistent with the CALL at (2)

这是代码模块:

module doubleMod
implicit none

contains
function double (n)
implicit none
integer :: n, double

double = 2*n
write(*,*) double
end function double

end module doubleMod

调用它的文件:

program testMod

use doubleMod

implicit none
integer :: n = 3

call double(n)

end program testMod

最佳答案

Fortran 有两种主要的过程类型:函数和子例程。函数返回一个值,因此它们在表达式内部被调用。示例:

a = myfunc(b)
print*, myfunc(a)

子程序不返回值,需要调用它们:

call mysub(a, b)

尝试在表达式中调用函数或使用子例程是语法错误。

在您的情况下,您可以或者double 转换为子例程:

subroutine double(n)
implicit none
integer, intent(inout) :: n
n = 2 * n
end subroutine double

那么您对 ​​double调用 将导致 n 的值加倍。

或者您可以更改调用double的方式:

program testMod
use doubleMod
implicit none
integer :: n
n = 3
n = double(n)
print*, n ! prints 6
end program testMod

关于linux - 从模块调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com