home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.fortran
- 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
- From: lionel@quark.enet.dec.com (Steve Lionel)
- Subject: Re: function name as an argument
- Message-ID: <1992Dec14.150347.17156@e2big.mko.dec.com>
- Lines: 54
- Sender: usenet@e2big.mko.dec.com (Mr. USENET)
- Reply-To: lionel@quark.enet.dec.com (Steve Lionel)
- Organization: Digital Equipment Corporation, Nashua NH
- References: <1992Dec8.051134.5968@news.Hawaii.Edu> <ADK.92Dec12114449@ds2.sun13.SCRI.FSU.EDU>
- Date: Mon, 14 Dec 1992 15:03:47 GMT
-
-
- In article <ADK.92Dec12114449@ds2.sun13.SCRI.FSU.EDU>,
- adk@sun13.SCRI.FSU.EDU (Tony Kennedy) writes:
- |>>> J. Giles <jlg@cochiti.lanl.gov> and
- |>>> Steve Lionel <lionel@quark.enet.dec.com> write:
- |>
- |> lionel> PROGRAM DUMB
- |> lionel> character*10 func,arg
- |> lionel> external func
- |> lionel> call sub(func,arg)
- |> lionel> end
- |>
- |> lionel> SUBROUTINE SUB (func,arg)
- |> lionel> character*(*) func,arg
- |> lionel> arg = func()
- |> lionel> write(*,*)arg
- |> lionel> END
- |>
- |> lionel> FUNCTION FUNC()
- |> lionel> character*(*) func
- |> lionel> func = 'abcdefghijklmnopqrstuvwxyz'
- |> lionel> END
- |>
- |> lionel> How does SUB know how much to allocate for the function result
- |> lionel> of SUB? This isn't known at compile time, so the length has to
- |> lionel> be passed in to SUB somehow so that SUB can allocate the space
- |> lionel> at run-time.
- |>
- |> jlg> Nope, the space can be allocated statically when PROGRAM DUMB
- |> jlg> is compiled! The descriptor passed to SUB can contain the
- |> jlg> length of the space which has been allocated and a reference
- |> jlg> to that memory. This is the same information that needs to
- |> jlg> be passed if FUNC were called *directly* by the main program.
- |>
- |>This is not possible because the three routines can be compiled separately.
- |>
-
- Absolutely true. But even if you compiled all the routines together, what if
- you added another caller of SUB which passed FUNC declared as a different
- length? Unless you additionally require procedure inlining, there is no way
- that SUB can know which routine called it and which length to statically
- allocate. This is why the standard requires the length of a character
- function to be declared as an integer constant expression in the program
- unit which calls the function. And this is why an implementation (such as
- VAX FORTRAN), which does allow the (*) in the referencing unit, passes the
- length along with the routine address, so that it may do run-time allocation
- for the function result.
- --
-
- Steve Lionel lionel@quark.enet.dec.com
- SDT Languages Group
- Digital Equipment Corporation
- 110 Spit Brook Road
- Nashua, NH 03062
-