Path: utzoo!attcan!uunet!husc6!uwvax!dogie!uwmcsd1!csd4.milw.wisc.edu!markh From: markh@csd4.milw.wisc.edu (Mark William Hopkins) Newsgroups: comp.lang.pascal Subject: The gaping hole left by var parameters ... Summary: Aliasing, side-effects Message-ID: <5879@uwmcsd1.UUCP> Date: 25 May 88 04:20:21 GMT Sender: daemon@uwmcsd1.UUCP Reply-To: markh@csd4.milw.wisc.edu (Mark William Hopkins) Organization: University of Wisconsin-Milwaukee Lines: 100 What's wrong with var parameters? Try these out to see how they compile and run: Enjoy ... (1) ALIASING: procedure Alias1(var X, Y : integer); begin X := X + 1; Y := X + 1 end; ... begin A := 3; Alias1(A, A); writeln(A) end. You might say that the whole is more than the sum of its parts ... -------------------------------------------------------------------------------- procedure Alias2(X : integer; var Y : integer); begin X := X + Y + 1; A := A + Y + 1 end; ... begin A := 3; Alias2(A, A); writeln(A) end. -------------------------------------------------------------------------------- (2) INSIDUOUS SIDE-EFFECTS: function Insiduous1(var X, Y : integer; Z : integer) : boolean; begin X := X + 1; Y := Y + X; Insiduous1 := (X <= Z) end; ... begin A := 0; B := 0; C := 5; while Insiduous1(A, B, C) do {nothing}; writeln('The sum of the integers from 0 to ', C:1, ' is :', B) end. -------------------------------------------------------------------------------- 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? -------------------------------------------------------------------------------- function Insiduous2(var X : integer) : integer; var Dummy : integer; begin if X >= 0 then begin writeln('The final result is :', X:1) Insiduous2 := X end else begin Dummy := X + Insiduous2(X + 1); writeln('The intermediate result was :', Dummy); Insiduous2 := Dummy end end; begin A := -10; writeln(Insiduous2(A)) end. This illustrates that an entire program could effectively be executed in one write statement. -------------------------------------------------------------------------------- String processing in Pascal?! function String : char; const PERIOD = '.'; begin write('This is an arbitrary string'); String := PERIOD end ... begin writeln(String) end; The function declaration is effectively the same as the assignment: String := 'This is an arbitrary string.';