- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear all,
I would like to write a function that will return a string, but I do not want the string contains trailing spaces. How could I do it? Thanks!
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
function foo()
character(:), allocatable :: foo
foo = "abc"
end function foo
You must have an explicit interface for this visible to the caller - the easiest way to do that is to put the function in a module and then USE the module in the program units where you want to call the function.
Please note also that you need a reasonably current compiler (12.1 minimum, I think), for this to work.
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You could write a specification function that computes the length of the function result. Sometimes this isn't feasible, though, for example when the function returns the next line of input. In this case the function result may be declared as allocatable length. In either case the interface must be explicit in the calling program unit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can also do it with a function that returns a deferred-length allocatable character result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can also do it with a function that returns a deferred-length allocatable character result.Can you give me an example solution of this: character(1000) function foo() foo = "abc" end function foo where I would like foo() to return "abc", not "abc" with huge number of spaces.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
function foo()
character(:), allocatable :: foo
foo = "abc"
end function foo
You must have an explicit interface for this visible to the caller - the easiest way to do that is to put the function in a module and then USE the module in the program units where you want to call the function.
Please note also that you need a reasonably current compiler (12.1 minimum, I think), for this to work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, Steve!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am still having a problem as:
function foo()
character(:), allocatable :: foo
character(1000) bar
bar = "abc"
foo = trim(bar)
end function foo
The content of foo still includes trailing spaces.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I tried it and it worked correctly for me. Here's the test program I used and its output:
print '(A,A,A)', '*',foo(),'*'
contains
function foo()
character(:), allocatable :: foo
character(10) bar
bar = "abc"
foo = trim(bar)
end function foo
end
*abc*
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, again! "ifort" can behave properly, but "gfortran" still add the trailing spaces!
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