gpt4 book ai didi

fortran - Fortran 函数可以返回多个变量吗?

转载 作者:行者123 更新时间:2023-12-02 12:07:48 25 4
gpt4 key购买 nike

在下面的示例函数中,如何判断 abc 是输入值还是输出值,而不是来自上下文?

此外,如果调用 example() 是在 example() 范围之外分配给 c 的值?

function example(a, b, c)
real*8 a, b, c
c = a + b
example = c
end function example

最佳答案

Fortran 可以指定意图属性:

The INTENT attribute specifies the intended use of a dummy argument. An INTENT (IN) dummy argument is suitable for receiving data from the invoking scoping unit, an INTENT (OUT) dummy argument is suitable for returning data to the invoking scoping unit, and an INTENT (INOUT) dummy argument is suitable for use both to receive data from and to return data to the invoking scoping unit.

此外,还可以指定结果属性或为函数指定类型,所以可以这样写:

double precision function example(a, b, c)
implicit none
double precision, intent(in) :: a, b
double precision, intent(out) :: c
c = a + b
example = c
end function example

function example(a, b, c) result(res)
implicit none
double precision, intent(in) :: a, b
double precision, intent(out) :: c
double precision :: res
c = a + b
res = c
end function example

注意:我还添加了“隐式无”,并用 double 替换了 real*8。

关于fortran - Fortran 函数可以返回多个变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528446/

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