From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 21 Jul 93 15:26:01 GMT From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland. reston.ans.net!usc!elroy.jpl.nasa.gov!swrinde!network.ucsd.edu!news.cerf.net!sh rike.irvine.com!adam@ucbvax.Berkeley. (Adam Beneschan) Subject: Re: gethostname (Was: SunAda question) Message-ID: List-Id: In article <9307202026.aa28700@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes: > The function gethostname is defined as : > > function gethostname(name: address; namelen: integer) > return status_code; > > > > > Turns out that this function is not a function. It actually returns TWO > values! Here is an example program that will correctly use this "function. " > > function Get_Host_Name return String is > > length : INTEGER := 64; > hostname : STRING(1..Length); > result : status_code; > counter : integer; > > begin > result := unix.gethostname(hostname(1)'address, length); > if result = Unix.OK then > for i in 1 .. length loop > if hostname (i) = ascii.nul then > counter := i - 1; > exit; > end if; > end loop; > return hostname(1 .. counter); > else > return ""; > end if; > end Get_Host_Name; > > > That's right, after the call to gethostname, the variable hostname > has been modified to contain a string which is the hostname of the > machine that the program is running on... > > Frankly, I'm offended! This completely violates everything that I've > been taught about Ada functions! The actual parameters of a function > should not be modified. A function should only return one value. > > Wow, this is ugly! (Me thinks me smells a C programmer lurking > behind that "function"...) No need to get offended. This function is obviously a direct interface to the UNIX system call. (I.e., no Ada code was written; the function was declared with PRAGMA INTERFACE or the equivalent.) When you're interfacing to an external routine that returns a value (presumably in a register), but also modifies its parameters, you really have no choice except to declare a function that modifies its parameters. It's ugly, but it's really the only thing you can do. (VAX Ada provides a neat variation of PRAGMA INTERFACE in which you can declare it as an Ada *procedure*, and declare an OUT parameter to hold the value returned in the register; but I haven't seen this in any other Ada.) Yes, the Unix package could have hidden the implementation details in the package body and provided a better-looking Ada interface. But, I suppose, there's an advantage to allowing the programmer direct access to the UNIX system functions, without the overhead involved in "Adaifying" it. -- Adam -- Standard disclaimers, with a few non-standard ones thrown in just to confuse you.