函数结果没有隐式类型 [英] Function result has no implicit type
问题描述
以下是解决我遇到的问题的示例代码。我得到的错误消息是
lockquote
函数结果'sample'at(1)没有IMPLICIT类型。
我在line(1)的下方标注了标签。 https://stackoverflow.com/questions/17751863/function-has-no-implicit-type\">这个其他问题,但我无法弄清楚。这个函数在我的程序中的一个模块中,我确保模块的包含
,并且在这个函数之后结束模块。
我也在这个函数中使用 implicit none
,所以我不确定为什么我会得到这个消息。
添加真实
或复杂$ c如何修复此错误讯息? $ c>在
function
之前工作,但我真的不明白为什么。我不应该只能使用复杂的函数,因为数组在函数内部是复杂的吗?哪个更适合我的实际功能?两者都不会产生编译错误。
实函数示例(func)!这是行(1)
!复数函数示例(func)
隐式无
整型:: n,m
real :: x,y
复杂,维(-9:9 ,-9:9),intent(in):: func
复合体,维(-9:9,-9:9):: LocalF
LocalF = func
do n = -9,9
do m = -9,9
x = real(n)* 0.2
y = real(m)* 0.2
LocalF(n,m)= cmplx(z1(x,y),z2(x,y))!假设z1,z2定义良好
end do
end do
end函数示例
Fortran每个函数都有一个结果。如果你喜欢,你可以把结果看作函数返回的值。与Fortran程序中的其他值一样,函数结果也有类型,类型和等级。
默认情况下,函数结果与函数具有相同的名称本身,并且它的声明被添加到函数声明中。例如,这里
整数函数add(m,n)
整数,intent(in):: a, b
add = a + b
结束函数
函数被称为 add
,您可以看到(a)结果是整型(以及缺省类型和标量)和(b)结果是通过将两个参数相加形成的。
对于返回数组的函数,这个语法不可用,所以你不能写类似于
integer(1:4)add_vec(m1,m2)
在这种情况下,您必须明确定义结果变量的名称(以及后面的类型和类型)。坚持一个简单的例子,像
函数add(m,n)结果(addvec)
整数,意图(in):: a(4),b(4)
integer,dimension(4):: addvec
....
结束函数
请注意,您没有定义结果的意图。
我认为,case sample
意在返回一个rank-2的复数值数组。我认为OP需要替换
函数Sample(func)!this is line(1)
$ c
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $函数示例(func )结果(LocalF)
并且看看结果如何。在这里,如果它不是很明显,你会发现结果名称不必与函数名称相同。
此外... 在函数工作前添加Real或Complex,但我真的不明白为什么。
它可能在编译意义上工作,但执行它会导致流泪。通过告诉编译器函数结果是 real
或 complex
值,您满足函数定义的语法要求。但是,如果没有为结果变量赋值( real
或 complex
声明))值(称为在OP的代码中,样本
)函数最多只会返回垃圾。
要在OP的原始代码中尽量清楚代码有两个严重的错误:
- 函数(result)没有给出显式类型,这导致显示编译器消息。
- 该函数不包括设置结果变量的值,即与函数名称相同的变量(如果没有
结果
子句)。
Below is a sample code that addresses the problem I am having. The error message I am getting is
Function result 'sample' at (1) has no IMPLICIT type.
I label where line (1) is below.
I tried to follow this other question, however I wasn't able to figure it out. This function is within a module in my program and I made sure that the module has contains
and I end the module after this function.
I also use implicit none
in this function so I'm not sure why I get this message. How can I fix this error message?
Adding Real
or Complex
in front of function
works, but I don't really get why. Shouldn't I only be able to use complex since the arrays are complex inside the function? Which is more suitable for my actual function? Both yield no compilation errors.
real function Sample(func) !this is line (1)
!complex function Sample(func)
implicit none
integer :: n,m
real :: x,y
complex, dimension(-9:9,-9:9), intent(in) :: func
complex, dimension(-9:9,-9:9) :: LocalF
LocalF = func
do n=-9,9
do m=-9,9
x = real(n)*0.2
y = real(m)*0.2
LocalF(n,m)= cmplx(z1(x,y),z2(x,y)) !assume z1,z2 are well defined
end do
end do
end function Sample
In Fortran every function has a result. If you like you can think of the result as a value returned by the function. Like every other value in a Fortran program a function result has a type, and a kind and a rank too.
By default the function result has the same name as the function itself, and its declaration is prepended to the function declaration. For example, here
integer function add(m,n)
integer, intent(in) :: a,b
add = a+b
end function
the function is called add
and you can see (a) that the result is of type integer (and of default kind and scalar) and (b) that the result is formed by adding the two arguments together.
For functions returning arrays this syntax is not available, so you couldn't write something like
integer(1:4) add_vec(m1,m2)
In such cases you have to explicitly define the name (and later type and kind) of the result variable. Sticking with the simple example, something like
function add(m,n) result(addvec)
integer, intent(in) :: a(4),b(4)
integer, dimension(4) :: addvec
....
end function
Notice that you don't define the intent of the result.
In OP's case sample
is, I think, intended to return a rank-2 array of complex values. I think OP needs to replace
function Sample(func) !this is line (1)
with
function Sample(func) result(LocalF)
and see how that goes. Here, if it is not evident already, you learn that the result name doesn't have to be the same as the name of the function.
Furthermore ... Adding Real or Complex in front of function works, but I don't really get why.
It might work in the sense of compiling, but executing it will lead to tears. By telling the compiler that the function result is either a real
or complex
value you satisfy the syntactical requirements for a function definition. But without assigning a (real
or complex
as declared) value to the result variable (called Sample
in OP's code) the function will, at best, return junk.
To be as clear as I can ... in OP's original code there were two serious mistakes:
- The function (result) was not given an explicit type, which lead to the compiler message shown.
- The function did not include setting the value of the result variable, i.e. the variable with the same name as the function (in the absence of the
result
clause).
这篇关于函数结果没有隐式类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!