Le 10/07/2015 20:57, Steve Kargl a écrit : > On Fri, Jul 10, 2015 at 06:20:47PM +0200, Mikael Morin wrote: >> >> I'm not completely convinced by the standard excerpts that have been >> quoted about this topic, as they don't have any explicit mention of >> allocatable variables/expressions. > > I did not quote 12.3.3 about "characteristics of function results", > which mentions the allocatable attribute. But, that is not > necessarily relevant. The pieces I quoted explicitly states > > "On completion of execution of the function, the value returned > is that of its function result. ... If the function result is > not a pointer, its value shall be defined by the function." Yeah, well, if the standard committee had allowed unallocated allocatable results, they would have put it here together with pointer, I guess.
> > The function not only needs to allocate memory, it needs to > assign it a value. In the following, if i <= 0, the function > result is not defined. > > module foo > contains > function bar(i) > integer, allocatable :: bar > integer, intent(in) :: i > if (i > 0) bar = i > end function bar > end module foo > > program test > use foo > integer j > j = bar( 3); print *, j > j = bar(-3); print *, j > end if > end program test > > Even if Andre developed a patch to allocate memory in > bar() for the i <= 0 case to prevent the segfault, the > function must return a value. What should that value be? Your example is, of course, 100% invalid; a value is needed to put in j. But the case is more debatable to me, if j is allocatable. In that case an unallocated bar() result could just make j unallocated. Mikael