Xref: utzoo comp.lang.c:8552 comp.lang.misc:1331 Path: utzoo!mnetor!uunet!husc6!necntc!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c,comp.lang.misc Subject: Re: The need for D-scussion (was Re: D Wishlist) Message-ID: <3177@haddock.ISC.COM> Date: 26 Mar 88 01:42:29 GMT References: <12176@brl-adm.ARPA> <1988Mar11.215238.976@utzoo.uucp> <719@l.cc.purdue.edu> <10763@mimsy.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 36 Summary: Returning multiple values In article <10763@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >In article <719@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) writes: >>Furthermore, returning a list of values for a function is far different from >>returning a structure. > >If so, the compiler should be improved. There is no theoretical difference. Or perhaps the language should be improved. In C, the div() function returns a struct. If I want to add the squares of the two components, I have to write either qr = div(x, y); z = qr.quot * qr.quot + qr.rem * qr.rem; or use a function int f(struct div_t qr) { return (qr.quot * qr.quot + qr.rem * qr.rem); } z = f(div(x, y)); The former requires a throwaway variable; the latter, a throwaway function. In Lisp, you're still returning an aggregate. Things are a little better, because you can tighten the scope: (setq z ((lambda (qr) (+ (* (car qr) (car qr)) (* (cdr qr) (cdr qr)))) (div x y))) In Forth, you can actually return multiple values (as opposed to returning a single value of aggregate type): x @ y @ div dup * swap dup * + z ! There's a certain elegance here which is lacking in the first two. The qr temporary has been absorbed by the syntax of the language, in exactly the same manner as the temporary results of the "*" operator in all three examples. I don't know if this is what Herman was talking about, but in this sense the current C model is lacking. Whether this could be "fixed" in a C-like language is another question. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint