Home > Archive > Fortran > January 2006 > How to pass a string to/from a function in 77
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to pass a string to/from a function in 77
|
|
| jane.sync@yahoo.com 2006-01-29, 6:59 pm |
| I'm trying to pass strings between functions in Fortran 77, but I can't
find any reference for this... Can anyone help with explaining how to
send a string to a function and have the function return a string? So
far I have this:
PROGRAM HELLOWORLD
IMPLICIT NONE
Character*256 MyFunc
PRINT *, 'Hello '
FunctionString()
END PROGRAM
FUNCTION FunctionString() RESULT(String)
Implicit None
Character(*) String
String = "World"
Return
End
| |
| Gary L. Scott 2006-01-29, 6:59 pm |
| jane.sync@yahoo.com wrote:
> I'm trying to pass strings between functions in Fortran 77, but I can't
> find any reference for this... Can anyone help with explaining how to
> send a string to a function and have the function return a string? So
> far I have this:
>
> PROGRAM HELLOWORLD
> IMPLICIT NONE
> Character*256 MyFunc
>
> PRINT *, 'Hello '
> FunctionString()
>
> END PROGRAM
>
> FUNCTION FunctionString() RESULT(String)
> Implicit None
> Character(*) String
> String = "World"
> Return
> End
>
The below is a little closer and works with a particular F95 compiler
(not F77). I would probably write the function more generically than this.
PROGRAM HELLOWORLD
IMPLICIT NONE
character(10) functionstring
PRINT *, 'Hello ',FunctionString()
END PROGRAM
FUNCTION FunctionString() RESULT(String)
Implicit None
Character(10) String
String = "World"
Return
End
--
Gary Scott
mailto:garyscott@ev1.net
Fortran Library: http://www.fortranlib.com
Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html
Why are there two? God only knows.
If you want to do the impossible, don't hire an expert because he knows
it can't be done.
-- Henry Ford
| |
| Gary L. Scott 2006-01-29, 6:59 pm |
| Gary L. Scott wrote:
> jane.sync@yahoo.com wrote:
>
>
> The below is a little closer and works with a particular F95 compiler
> (not F77). I would probably write the function more generically than
> this.
>
> PROGRAM HELLOWORLD
>
> IMPLICIT NONE
>
> character(10) functionstring
>
> PRINT *, 'Hello ',FunctionString()
>
> END PROGRAM
>
> FUNCTION FunctionString() RESULT(String)
>
> Implicit None
>
> Character(10) String
>
> String = "World"
>
> Return
>
> End
>
Sorry, probably should have been declared with length 5 rather than 10
in this example.
--
Gary Scott
mailto:garyscott@ev1.net
Fortran Library: http://www.fortranlib.com
Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html
Why are there two? God only knows.
If you want to do the impossible, don't hire an expert because he knows
it can't be done.
-- Henry Ford
| |
| Richard Maine 2006-01-29, 6:59 pm |
| Gary L. Scott <garyscott@ev1.net> wrote:
> jane.sync@yahoo.com wrote:
>
>
> The below is a little closer and works with a particular F95 compiler
> (not F77).
[code elided]
Just to point out what features are f90/f95 about this, it is
1. The RESULT clause on the funcion statement
2. Implicit none
3. Names longer than 7 characters.
4. Mixed case
Items 2-4 are almost universally implemented on later f77 compilers,
though not earlier ones. Item 1 is not so common with f77.
And I thought I'd explain the differences between Jane's original and
Gary's changed version.
The most important difference is just that Jane accidentally declared
the name MyFunc instead of functionstring in the main program. I assume
that was just an accident, as the myfunc bit makes no sense, there being
no connection between the name myfunc and anything else in the code.
The other difference is more subtle. Gary switched to using a fixed
length for the function result instead of character(*). The character(*)
declaration actually ought to have worked in Jane's original once she
changed the declaration of myfunc to one for functionstring. However, I
recommend against using character(*) function results (dummy arguments
are different from function results on this). Character(*) function
results are deleted from f95 (I think it was deleted - either that or
obsolescent) because they are obscure, tricky, and rarely used. I would
estimate that 90% of the times I have seen someone try to use the
feature, they misunderstood how it worked. I'm, impressed that Jane's
original was essentially correct, once the name in the declaration was
changed.
There really isn't a good way to deal with functions that retuen
variable length strings until f2003. Gary's version here just does a
fixed-length string. The character(*) "looks" like a variable length
form, but isn't (that's the mistake many people make with it). In f90,
and more so in f95, you can play games with specification expressions
for the length, but except in the simplest cases that is awkward in the
extreme. In f2003, you can finally do the job simply and cleanly with a
function result that is an allocatabel length string. Note that f95 plus
the allocatable TR isn't enough, because that TR doesn't cover
allocatable string lengths, which were and f2003 integration add-on.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
|
| jane.sync@yahoo.com wrote in message <1138554124.774660.56170@g14g2000cwa.googlegroups.com>...
>I'm trying to pass strings between functions in Fortran 77, but I can't
>find any reference for this... Can anyone help with explaining how to
>send a string to a function and have the function return a string? So
>far I have this:
>
> PROGRAM HELLOWORLD
> IMPLICIT NONE
> Character*256 MyFunc
>
> PRINT *, 'Hello '
> FunctionString()
You need to have an assignment for this, or include it in the PRINT statement.
Myfunc = FunctionString()
>
> END PROGRAM
As suggested befure, move this statemnent to the very end,
and insert a CONTAINS statement here. Same reason as before.
> FUNCTION FunctionString() RESULT(String)
> Implicit None
> Character(*) String
You need to specify a constant length for the result.
e.g., character(len=10) :: String
> String = "World"
> Return
> End
|
|
|
|
|