Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

fortran - Function result has no implicit type

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  1. The function (result) was not given an explicit type, which lead to the compiler message shown.
  2. 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).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...