comp.lang.ada
 help / color / mirror / Atom feed
From: seas.gwu.edu!mfeldman@uunet.uu.net  (Michael Feldman)
Subject: Re: and then
Date: 11 Apr 93 03:55:54 GMT	[thread overview]
Message-ID: <1993Apr11.035554.28207@seas.gwu.edu> (raw)

In article <1993Apr10.010355.4244@nosc.mil> sampson@nosc.mil (Charles H. Sampso
n) writes:
>
>     Back to the question.  In my programming style, a function is a sub-
>program whose _primary_ purpose is the calculation of a single value and a
>procedure is a subprogram whose _primary_ purpose is performing an action.
>(There's an unfortunate grey area in applying these rules.)  This allows
>for functions that have side effects and procedures that have only one
>formal parameter, of mode OUT.  I tend to minimize the side effects of
>functions, but I don't rule them out.
>
>     For example, in processing a character stream, I prefer to have a
>function named Next_character that returns the next character from the
>stream and moves the stream cursor so that character becomes the previous
>character, rather than a procedure named Get.
>
There's a more obvious example. Ada does not provide static local storage -
all local objects are automatic and get blown off the stack when the
block in which they are declared goes out-of-scope.

Consider how to write a pseudo-random number generator, or any other
similar function that needs one or more state variables. (In case
pseudo-random numbers are not your career, we can summarize it by saying
that the function needs to remember what it did last time").

The canonical Ada way to do this is to export the function from a package, 
hiding the state variable in the package body. This state variable is never 
seen by a client, so it's protected from outside damage, but it is still a 
nonlocal reference for the function, in other words a side effect.

Calling this function, say, 

  FUNCTION PseudoRandom(N: Natural) RETURN Natural;

then each successive call of PseudoRandom(M) returns a number in 0..M.

I claim that 

  X := PseudoRandom(10) - PseudoRandom(10);

will store different values in X depending upon whether the first
or the second call (left side or right side) is evaluated first.

Side effects are a two-edged sword: Ada rules make them sometimes
unavoidable, but they are dangerous nonetheless. And one had better
have one's eyes open. If it is important that the left side be evaluated
first, better declare Temp and write

  Temp := PseudoRandom(10);
  X := Temp - PseudoRandom(10);

Mike Feldman

PS - no, they are not changing this in 9X...

             reply	other threads:[~1993-04-11  3:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-04-11  3:55 Michael Feldman [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-04-12 18:38 and then Charles H. Sampson
1993-04-12 13:29 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!noc.n
1993-04-10 19:52 Alex Blakemore
1993-04-10 15:43 Dik T. Winter
1993-04-10 15:36 Dik T. Winter
1993-04-10  9:39 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!usenet
1993-04-10  1:03 Charles H. Sampson
1993-04-09 18:08 Dave Bashford
1993-04-09 14:06 Dan Rittersdorf
1993-04-08 22:35 Andrew Dunstan,,2285592,
1993-04-08 22:28 Alex Blakemore
1993-04-08 19:03 Art Duncan
1993-04-08 16:18 Charles H. Sampson
1993-04-08 15:34 Christopher J. Henrich
1993-04-08 12:21 enterpoop.mit.edu!usc!cs.utexas.edu!mars.tsd.arlut.utexas.edu!gardner
1993-04-07 22:58 Mark Lundquist
1993-04-07 21:07 Ray Harwood -- Data Basix: (602)721-1988
1993-04-07 16:21 Charles H. Sampson
1993-04-07 12:42 Robert Firth
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox