Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!tektronix!uunet!mcvax!diku!damm From: damm@diku.dk (Kristian Damm Jensen) Newsgroups: comp.lang.pascal Subject: Multiple function-values (was: Re: The gaping hole left by var parameters ...) Message-ID: <3854@diku.dk> Date: 29 May 88 14:35:13 GMT References: <5879@uwmcsd1.UUCP> <3499@omepd> <5915@uwmcsd1.UUCP> Organization: DIKU, U of Copenhagen, DK Lines: 65 In article <5915@uwmcsd1.UUCP> markh@csd4.milw.wisc.edu (Mark William Hopkins) writes: >In article <3499@omepd> bobdi@omepd.UUCP (Bob Dietrich) writes: << stuf deleted >> >You have a point on one matter: as long a Pascal does not allow files-as-values >side-effects must occur be allowed in functions. Also, as long as Pascal does >not allow the one to construct functions that can return MULTIPLE values, one >will need to write procedures with var parameters in their stead. This is not correct. A way to overcome this problem is to put those multiple values in a record, and let the function return a pointer to the record. Agreed, this doesn't seem very elegant, but it works, an you favor a LISP-like programming style, then it's very handy. >Example: > procedure Switch(var A, B : char); > var C : char; > begin > C := A; A := B; B := C > end; >called in the environment > Switch (X, Y) >would become: > function Switch(A, B : char) --> (char, char); > var C : char; > begin > C := A; A := B; B := C; > return (A, B) > end; >called as follows: > (X, Y) := Switch(X, Y) This example would then become Type TwoCharPointer = ^ThoCharRecord; TwoCharRecord = Record a, b : Char End; Function Switch (TwoChars : TwoCharRecord) : TwoCharPointer; Var help : Char; Return : TwoCharPointer; Begin With ThoChars Do Begin help := a; a := b; b := help End; Return^ := TwoChars; Switch := Return End; -- Kristian Damm Jensen (damm@diku.denet.uucp)