Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28701 Discussions

function returning allocatable array

MR
Beginner
1,040 Views
The intel compiler does not accept the following code:

module mod_test

implicit none

public :: f

private

interface f
module procedure ff
end interface

contains

pure function ff(n)
real, allocatable :: ff(:)
integer, intent(in) :: n

allocate( ff(n) )
ff = 1.0

end function ff

end module mod_test


Both ifort (IFORT) 10.0 20070809 and
ifort (IFORT) 9.1 20061101 give

fortcom: Error: mod_test.f90, line 19: This name does not have a type, and must have an explicit type. [FF]
allocate( ff(n) )
------------^
fortcom: Error: mod_test.f90, line 16: This ALLOCATABLE attribute is invalid; the ALLOCATABLE attribute can only be used when declaring array objects. [FF]
real, allocatable :: ff(:)
-----------------------^
fortcom: Error: mod_test.f90, line 15: The array-spec for a function name with no POINTER attribute must be an explicit shape array (R505.9). [FF]
pure function ff(n)
---------------^
fortcom: Warning: mod_test.f90, line 15: The return value of this FUNCTION has not been defined. [FF]
pure function ff(n)
---------------^
fortcom: Error: mod_test.f90, line 15: If a deferred-shape array is intended, then the ALLOCATABLE or POINTER attribute is missing; if an assumed-shape array is intended, the array must be a dummy argument.
pure function ff(n)
---------------^
compilation aborted for mod_test.f90 (code 1)


My system: Gentoo Linux, x86_64 AMD Turion

Notice that the problem disappears if the interface
block is eliminated. I suspect this is a bug in
the compiler, but of course I would appreciate if
you could confirm it (g95 and gfortran accept the code).

Best regards,
Marco Restelli
0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,040 Views
The following works:

pure function ff(n) result(res)
real, allocatable ::res(:)
integer, intent(in) :: n

allocate(res(n) )
res = 1.0

end function ff

I need to do some more reading to see if the original code is legal, but the fact that the compiler doesn't complain if the generic interface is removed leads me to suspect a compiler bug.
0 Kudos
Steven_L_Intel1
Employee
1,040 Views
I believe this to be a compiler bug and will let the developers know about it. Thanks.
0 Kudos
Reply