home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / fortran / 4695 < prev    next >
Encoding:
Text File  |  1992-12-14  |  2.7 KB  |  67 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!walter!att-out!pacbell.com!decwrl!deccrl!news.crl.dec.com!dbased.nuo.dec.com!e2big.mko.dec.com!quark.enet.dec.com!lionel
  3. From: lionel@quark.enet.dec.com (Steve Lionel)
  4. Subject: Re: function name as an argument
  5. Message-ID: <1992Dec14.150347.17156@e2big.mko.dec.com>
  6. Lines: 54
  7. Sender: usenet@e2big.mko.dec.com (Mr. USENET)
  8. Reply-To: lionel@quark.enet.dec.com (Steve Lionel)
  9. Organization: Digital Equipment Corporation, Nashua NH
  10. References: <1992Dec8.051134.5968@news.Hawaii.Edu> <ADK.92Dec12114449@ds2.sun13.SCRI.FSU.EDU>
  11. Date: Mon, 14 Dec 1992 15:03:47 GMT
  12.  
  13.  
  14. In article <ADK.92Dec12114449@ds2.sun13.SCRI.FSU.EDU>, 
  15. adk@sun13.SCRI.FSU.EDU (Tony Kennedy) writes:
  16. |>>> J. Giles <jlg@cochiti.lanl.gov> and
  17. |>>> Steve Lionel <lionel@quark.enet.dec.com> write:
  18. |>
  19. |>   lionel> PROGRAM DUMB
  20. |>   lionel> character*10 func,arg
  21. |>   lionel> external func
  22. |>   lionel> call sub(func,arg)
  23. |>   lionel> end
  24. |>
  25. |>   lionel> SUBROUTINE SUB (func,arg)
  26. |>   lionel> character*(*) func,arg       
  27. |>   lionel> arg = func()
  28. |>   lionel> write(*,*)arg
  29. |>   lionel> END
  30. |>
  31. |>   lionel> FUNCTION FUNC()
  32. |>   lionel> character*(*) func
  33. |>   lionel> func = 'abcdefghijklmnopqrstuvwxyz'
  34. |>   lionel> END
  35. |>
  36. |>   lionel> How does SUB know how much to allocate for the function result
  37. |>   lionel> of SUB?  This isn't known at compile time, so the length has to
  38. |>   lionel> be passed in to SUB somehow so that SUB can allocate the space
  39. |>   lionel> at run-time.
  40. |>
  41. |>   jlg> Nope, the space can be allocated statically when PROGRAM DUMB
  42. |>   jlg> is compiled! The descriptor passed to SUB can contain the
  43. |>   jlg> length of the space which has been allocated and a reference
  44. |>   jlg> to that memory.  This is the same information that needs to
  45. |>   jlg> be passed if FUNC were called *directly* by the main program.
  46. |>
  47. |>This is not possible because the three routines can be compiled separately.
  48. |>
  49.  
  50. Absolutely true.  But even if you compiled all the routines together, what if
  51. you added another caller of SUB which passed FUNC declared as a different
  52. length?  Unless you additionally require procedure inlining, there is no way
  53. that SUB can know which routine called it and which length to statically
  54. allocate.  This is why the standard requires the length of a character
  55. function to be declared as an integer constant expression in the program
  56. unit which calls the function.  And this is why an implementation (such as
  57. VAX FORTRAN), which does allow the (*) in the referencing unit, passes the
  58. length along with the routine address, so that it may do run-time allocation
  59. for the function result.
  60. --
  61.  
  62. Steve Lionel                    lionel@quark.enet.dec.com
  63. SDT Languages Group
  64. Digital Equipment Corporation
  65. 110 Spit Brook Road
  66. Nashua, NH 03062
  67.