Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!amdcad!sun!pitstop!sundc!seismo!uunet!littlei!intelisc!omepd!bobdi From: bobdi@omepd (Bob Dietrich) Newsgroups: comp.lang.pascal Subject: Re: The gaping hole left by var parameters ... Summary: Ain't nuthin new Message-ID: <3499@omepd> Date: 26 May 88 18:00:52 GMT References: <5879@uwmcsd1.UUCP> Reply-To: bobdi@omepd.UUCP (Bob Dietrich) Organization: Intel Corp., Hillsboro, Oregon Lines: 70 In article <5879@uwmcsd1.UUCP> markh@csd4.milw.wisc.edu (Mark William Hopkins) writes: >What's wrong with var parameters? >Try these out to see how they compile and run: >Enjoy ... I see you've rediscovered aliasing and functions having side-effects, but you're certainly not pointing out anything new. Languages that have a variable parameter mechanism and scoping have always had this problem, and it isn't likely to go away. Likewise functions with side-effects. Both mechanisms have their drawbacks, but are generally too useful to plug all the holes (the old "double-edged sword" argument). Attempts are made from time to time, but they usually just force the programmer into an alternate and perhaps less natural construct (like returning a result in a parameter instead of as a function result). Reminds me of a Pascal compiler brought out a few years ago by a semiconductor company (not my current employer). The implementors had read that functions with side-effects are evil, and so their functions disallowed side-effects (Hmmmm. Did they disallow all file operations? I can't remember). Anyway, it turns out they also distended procedures so that they could return values just like functions. So if you wanted a function with side-effects, you had to call it a procedure returning a value. Someone wanted good marketing copy, but also saw a need to be filled. See the Pascal Validation Suite for many dandy examples of aliasing and side-effects. > [...] >-------------------------------------------------------------------------------- > function ForLoop(var I, Bound : integer) : boolean; > begin > ForLoop := (I < Bound); > I := I + 1 > end; > > ... > > begin > I := 0; while ForLoop(I, 3) do A[I] := 2*I; > I := 0; while ForLoop(I, 3) do write(A[I]:1, ' '); > writeln > end. > >Why even use a for loop with this dandy available? I sure hope this fragment doesn't make it through any purported Pascal processor you have! For the invocations of ForLoop to work, the second parameter should be a value parameter, and the declaration as follows: function ForLoop(var I: integer; Bound : integer) : boolean; As far as the question goes, when I actually DO use a for-statement, I want to use it for other than counting integers upwards. I'm just as likely to use a for-statement with an enumeration, subrange, or the char type. My programs have few if any variables declared as integer; instead, I use subranges. I don't know how the variable A is declared in your fragment, but it looks like its index type begins at one; therefore, any variable used to index A should probably have a lower bound of one as well. Since Pascal uses name type compatibility, your function would have few if any uses in any of my programs. You might also note that a function like this will get errors if the counter variable is at the last value of its type (e.g., "ForLoop(I, maxint)"). Bob Dietrich Intel Corporation, Hillsboro, Oregon (503) 696-4400 or 2092(messages x4188,2111) usenet: tektronix!ogcvax!omepd!bobdi or tektronix!psu-cs!omepd!bobdi or ihnp4!verdix!omepd!bobdi