问题描述
出于优化原因,Fortran强制执行子例程或函数的伪参数不是别名,即它们不指向相同的内存位置。我想知道是否相同的约束适用于函数的返回值。
换句话说,对于给定的myfunc函数:
function myfunc(a)
real,intent (in):: a(:)
real :: myfunc(size(a))
myfunc = a * 2
结束函数myfunc
是否符合标准要求:
a = myfunc(a)
和
b = myfunc(a)?
函数和函数返回值的参数是不同的东西。与之前的答案相反,函数参数是通过引用传递的,或者通过copy-in copy-out传递的,除非它们被声明为具有 VALUE
属性的虚拟参数。这是Fortran与C的主要区别。
但是,如果函数值是由正常赋值(=)而不是由指针赋值(=>)构造的,是独立的实体。在你的代码中,myfunc的值是通过复制a的值得到的。因此,没有标准规则被 a = myfunc(a)
或 b = myfunc(a)
。
For optimisation reasons, Fortran enforces that the dummy arguments of a subroutine or function are not alias, i.e., they do not point the the same memory place.
I am wondering if the same constraint applies to the returned value of a function.In other words, for a given myfunc function:
function myfunc(a)
real, intent(in) :: a(:)
real :: myfunc(size(a))
myfunc = a * 2
end function myfunc
is it standard-compliant to write:a = myfunc(a)andb = myfunc(a) ?
The arguments of a function and function return value are different things. Contrary the previous answer, the functional arguments ARE passed by reference, or by copy-in copy-out, unless they are declared as dummy arguments with the VALUE
attribute. This is a major difference of Fortran vs. C.
However, if the function value is constructed by normal assignment (=) and not by pointer assignment (=>) they are separate entities. In your code, the value of myfunc is got by copying the value of a. Therefore no standard rules are broken by a = myfunc(a)
or b = myfunc(a)
.
这篇关于Fortran函数中的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!