home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / fortran / 4665 next >
Encoding:
Internet Message Format  |  1992-12-11  |  4.3 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!sampson!shepard
  2. From: shepard@sampson.ccsf.caltech.edu (Ron Shepard)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: function name as an argument
  5. Date: 11 Dec 1992 18:05:10 GMT
  6. Organization: California Institute of Technology, Pasadena
  7. Lines: 98
  8. Message-ID: <1gal8nINNre4@gap.caltech.edu>
  9. References: <1992Dec8.140008.20523@e2big.mko.dec.com> <BURLEY.92Dec8185030@apple-gunkies.gnu.ai.mit.edu> <1992Dec10.012254.3535@alchemy.chem.utoronto.ca>
  10. NNTP-Posting-Host: sampson.ccsf.caltech.edu
  11.  
  12. mroussel@alchemy.chem.utoronto.ca writes:
  13. >In article <BURLEY.92Dec8185030@apple-gunkies.gnu.ai.mit.edu>
  14. >burley@apple-gunkies.gnu.ai.mit.edu (Craig Burley) writes:
  15. >>In article <1992Dec8.140008.20523@e2big.mko.dec.com> lionel@quark.enet.dec.com
  16. >>(Steve Lionel) writes:
  17. >>   Typically, the argument passed is the address of the routine's entry point.
  18. >>   If the argument has CHARACTER datatype, the length is also passed by some
  19. >>   implementation-dependent method.   Note that you should declare a routine as
  20. >>   EXTERNAL in order to pass it as an argument.
  21. >>
  22. >>I'm unaware of any need for the length of a CHARACTER FUNCTION argument
  23. >>to be passed to a called procedure in the ANSI FORTRAN 77 standard.
  24. >>As far as I know, the actual caller of the function (via a dummy) must
  25. >>know (or declare) the length of the returned value.
  26. >
  27. >     Consider the following bit of code which, I believe, is perfectly
  28. >standard conforming:
  29. >
  30. >       PROGRAM DUMB
  31. >       character*10 func,arg
  32. >       arg = func()
  33. >       write(*,*)arg
  34. >       END
  35. >
  36. >       FUNCTION FUNC()
  37. >       character*(*) func
  38. >       func = 'abcdefghijklmnopqrstuvwxyz'
  39. >       END
  40. >
  41. >The function must know len(func) since it must not overwrite memory past
  42. >the end of that assigned to the return value.  In other words, some
  43. >mechanism must exist for FUNC to know its declared length.  One way to
  44. >do this is for len(func) to be passed as a supplementary, invisible (to
  45. >the programmer) parameter to the function call.
  46.  
  47. This is right, but it doesn't address the original question which
  48. was about passing character functions as arguments.  As I understand
  49. f77, the code
  50.  
  51.       program dumb
  52.       character*10 cfun, cvar
  53.       external cfun
  54.       call sub( cvar, cfun )
  55.       write(*,*) 'cvar=', cvar
  56.       stop
  57.       end
  58.       subroutine sub( cvar, cfun )
  59.       character*(*) cvar, cfun
  60. c     # the following statement is not legal with *(*) declaration
  61. c     # but is legal with *10 declaration.
  62.       cvar = cfun()
  63.       return
  64.       end
  65.       function cfun
  66.       character*(*) cfun
  67.       cfun = '12345678901234567890'
  68.       return
  69.       end
  70.  
  71. is not legal for the reason given above in the comments.  In fact, if
  72. the argument list to sub() is extended to include the length variable,
  73. it is STILL not legal.
  74.  
  75.       ...
  76.       call sub( cvar, cfun, 10 )
  77.       ...
  78.       subroutine sub( cvar, cfun, len )
  79.       character*(len) cvar, cfun
  80.       ...
  81.  
  82. The only way to make it legal is to use a constant, or a parameter,
  83. in the type declaration within sub().  Note that it is the function
  84. "cfun()" that causes the problems, not the variable "cvar".  All of
  85. the above (e.g. *(*), *10, *(len)) works fine with "cvar".
  86.  
  87. The second paragraph of the standard on page 15-2 states: "If a
  88. character function is referenced in a program unit, the function
  89. length specified in the program unit must be an integer constant
  90. expression."  This explains why "*10" is the only legal declaration
  91. for "cfun()", whereas all three types of declarations are fine with
  92. "cvar".  
  93.  
  94. Note that it is the REFERENCING of cfun() that is illegal, not the
  95. declaration itself.  This means that external cfun() could be declared
  96. as *(*) within sub(), and used as an argument in another call statement.
  97. As long as it is declared as *(CONSTANT) in the subprogram in which it
  98. is finally referenced, everything is alright.
  99.  
  100. The next question, of course, is why was this additional restriction
  101. imposed on functions?  Maybe Burley can give some insight here, but my
  102. guess is that the space associated with cfun() is allocated within
  103. sub() instead of in the main program (where the space for cvar() is
  104. associated.  Otherwise, expressions involving cfun() might require
  105. runtime memory allocation, something which is carefully avoided in
  106. character manipulations in f77.
  107.  
  108. -ron shepard
  109. shepard@tcg.anl.gov
  110.