09-261 To: J3 From: John Reid Subject: Iterp F95/0098 Date: 2009 July 24 This is an edited version that now refers to Fortran 2003. NUMBER: F95/0098 TITLE: Are dummy functions returning assumed-length character legal? KEYWORDS: DUMMY FUNCTION, ASSUMED-LENGTH CHARACTER DEFECT TYPE: Interpretation STATUS: J3 consideration in progress QUESTION: Are dummy functions returning assumed-length character legal? Consider the following program: character*5 f external f call ss3(f) end subroutine ss3(ff) character*(*) ff, a*10 external ff a = ff()//'99999999' print *, '<', a, '>' end function f() character*(*) f f = '123' end The issue is the call to the dummy function 'ff' in subroutine 'ss3'. Some compilers issue an error message. Others print the result: <123 99999> There appear to be two relevant, contradictory passages in the F2003 standard. Refer to section 4.4.4.1. <> means italics, [] means obsolescent font. C417 says "A function name shall not be declared with an asterisk unless it is of type CHARACTER and is the name of the result of an external function or the name of a dummy function. At the end of the section, there is the text "A char-length type parameter value of * has the following meaning: (1) ... (2) ... (3) ... (4) [If used to specify the character length parameter of a function result, any scoping unit invoking the function shall declare the function name with a character length parameter value other than * or access such a definition by host or use association. When the function is invoked, the length of the result variable in the function is assumed from the value of this type parameter.]" According to the standard, the use in the example program is legal, because 'ff' is the name of a dummy function. But it seems clear that the standard intended that the length of the variable returned by a character function be known at compile time; it is not known in the case where the function is a dummy function. ANSWER: Yes, dummy functions returning assumed-length character are permitted, but in any scoping unit that invokes the function, the actual length must be declared or accessed by use or host association, see item (4) in the final paragraph of 4.4.4.1, which is quoted above. The program therefore does not conform with the standard since the function is invoked from the scoping unit of ss3 within which it is declared with a character length parameter value of *. An edit is provided to make this clear. A further problem is that there is no requirement for the type and type parameters of a dummy function with an implicit interface to agree with those of the actual argument. It is intended that there be full agreement except in the case of an actual argument with assumed character length and a dummy argument of explicit character length. An edit is supplied to correct this. With this edit in place, the invocation of ss3 in the above example does not conform with the standard. If the main program had been changed to character*(*) f external f call ss3(f) end it would not have conformed to the standard since C416 and C417 do not permit it. This is a mistake and edits are supplied to correct it. EDITS: Subclause 4.4.4.1 [41:6] In C416, item (1), change "dummy argument" to "dummy argument [or external function]". [41:12] In C417, change "[a dummy]" to "[a dummy or an external]". [41:34-37] Replace item (4) of the final paragraph by "[If used to specify the character length parameter of a function result, for each invocation of the function, there is a value such that had the character length of the result variable been declared with this value in the function, the invocation would be valid. The length of the result variable in the function is assumed to have this value.]". Subclause 12.4.1.3 [271:28] At the end of paragraph 5, add ", and shall have the same type and type parameters [except that an actual argument with assumed character length may be associated with a dummy argument with explicit character length]". SUBMITTED BY: Larry Meadows HISTORY: 02-157 m160 F95/0098 submitted 04-417r1 m170 Duplicate of interp #6 05-180 m172 Failed WG5 ballot N1617 - not a duplicate of F95 interp 000006 ........................................................................ John Reid NO vote on 000098 This is different from interpretation 6 since the function has assumed character length, which means that item (4) of page 41 applies. Hence, I think the ANSWER should be The program does not conform with the standard because of the rule in item (4) of the second list in 4.4.4.1. The function f is invoked from the scoping unit of ss3 within which the function name does not satisfy any of the required conditions. Malcolm Cohen NO vote on F95/000098 I agree with John that there is an issue here which is not adequately covered by Interp F95/000006. However, I think that the suggestion that item (4) of 4.4.4.1 means that the program is invalid is without merit. A literal interpretation of item (4) means that even if subroutine SS3 declared dummy FF as being CHARACTER*5, the program would still be invalid since the name of the function is F, not FF. This would be a completely ridiculous result. Thus item (4) of 4.4.4.1 is faulty and an edit is required for it to handle (assumed-length) dummy functions at all.