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

Type mismatch error when calling function

littleprince
Beginner
3,615 Views

When I run this simple program, I get an error: function getlength has no implicit type, type mismatch (unknown/integer). (I understand that there is no real need for the function at all, I could just use len() in the main. This program is for test purposes only.) What am I doing wrong/how can I fix it? Thanks in advance for your help!

program test
implicit none
character(len = 100) :: input
integer :: length = 0
read *, input
length = getLength(input)
print *, length
end


integer function getLength(array)
character(len = 100) array
getLength = len(array)
end
0 Kudos
3 Replies
mecej4
Honored Contributor III
3,615 Views
You have to declare getLength as type INTEGER in the main program. Without such a declaration, the implicit typing rules make it REAL, which clashes with the later declaration of the function as INTEGER.

There is a bigger problem with your code. The function will return 100 instead of what you may have expected. You need to read books/manuals to see how character strings are handled in Fortran. This problem will not go away even if you replace the reference to getLength by one to the intrinsic LEN function.
0 Kudos
littleprince
Beginner
3,615 Views
mecej4, thanks for replying to my post. You are correct that my function getLength will always return 100; this program I posted is a greatly simplified form of the real thing. How do I declare getLength as an integer in the main program? will "external integer getLength" work?
0 Kudos
TimP
Honored Contributor III
3,615 Views
If you want to put external and integer in the same declaration, you need f90 style with the comma and double colons. f77 style requires 2 separate lines.
external is usually considered good form, although often omitted.
0 Kudos
Reply