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

Compiler Error: A CHARACTER function name must not be declared...

ferrad01
Beginner
933 Views

What does this mean, and how can I avoid it?

character*(*) function gaw(line,separ,number)

implicit none ...

Error: A CHARACTER function name must not be declared with an asterisk type-param-value (i.e., (LEN=*)) if the function is a module function. [GAW]

This works fine in CVF.

0 Kudos
6 Replies
Steven_L_Intel1
Employee
934 Views
Well, it's not allowed by the language. The rule says:

A function name shall not be declared with an asterisk type-param-value unless it is of type CHARACTER and is the name of the result of an external function or the name of a dummy function.

A module function is not an external function. The standard is trying to get rid of character(*) functions. Offhand, I can't think of what problem they may cause, but I guess we chose to follow the standard here.
0 Kudos
jimdempseyatthecove
Honored Contributor III
934 Views

Steve,

This seems like a shortcomings of the language. i.e. there is no means for the character function to determine the length of the buffer to which it references. May be cause for buffer overrun.

It would seem like a good way to handle this is to require an interface block for such functionsand to place the "descriptor" (or reference to the descriptor) on the call stack then redefine the function name inside the function with pointer attribute and associate the function name (now pointer to character array) with the hidden argument(descriptor).

Jim

0 Kudos
TimP
Honored Contributor III
933 Views

Adams, Brainerd et al "Fortran 95 Handbook" show an example of a function using result to return a character string whose length is set to the length of the function argument, with CONTAINS.
0 Kudos
Steven_L_Intel1
Employee
934 Views
Yes - using a specification expression to set the length makes more sense and is the recommended approach.. The old CHARACTER(*) FUNCTION syntax was very awkward and required the caller to explicitly declare the function with a specific length.

For example, you can do this:

function func(arg)
character(*), intent(in) :: arg
character(len(arg)) :: func

0 Kudos
jimdempseyatthecove
Honored Contributor III
934 Views

The point was not to have to explicitlypass the length of the buffer referenced by the function. Nor implicitly assume the length of the output buffer is related to the input arg(s).

Assume

character :: buf(6)
real :: PI =3.14159
...
buf = MyFortranFunctionToConvertToString(PI)

The character function should have a goof proof method of obtaining the destination buffer size. Requiring the use of a dummy argument for this purpose is not goof proof.

buf = MyFortranFunctionToConvertToString(PI,size(buff))

Note type-o on line. If you are going to add an argument you might as well use a CALL

Requiring the user to write the conversion function as a CALL (using 2 args) is a bother.

I guess if you want portable code then stick with the worst common denominator...

Now where did I place that FORTRAN-II programmers manual.

Jim


0 Kudos
Steven_L_Intel1
Employee
933 Views
Jim, I don't understand your post - or perhaps you didn't understand mine.

With CHARACTER(*) functions, the caller must explicitly declare the one and only result length that will be returned when that function is called This is an awkward thing to do and you often don't know at compile-time what that length should be. (If you did, perhaps you should declare the function with a fixed length.

Being able to use a run-time expression for the length is quite liberating. How you determine that length is up to you - most applications do want it to be based on the length of an argument, but you could use a COMMON or module variable too if you liked.
0 Kudos
Reply