- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
external is usually considered good form, although often omitted.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page