scm3775@tamsun.tamu.edu (Sean Malloy) (03/28/91)
I just posted (most of) this article. Then I realized that I didn't clarify my position well enough. Welcome to V1.1. Sorry to waste bandwidth with such a trivial question, but it will settle an argument, and it *is* about C. Given the following fragment: char arrb[] = "mine"; /* not global */ fcn_c( arrb ); ------- int fcn_c (char arrb []) { arrb[3] = 't'; } Is the above an example of call by value or call by reference? My friend insists that it is a call by value, since fcn_c gets a *copy* of the pointer arrb. However, he believes that C *can* make calls by reference. If he's to have that belief, doesn't it look like this is one of the occasions that *must* be a 'call by reference'? (Yes, I know that K&R says that all calls are by value. What I'm saying here is: ASSUME that C can have calls by reference. Wouldn't the example above be an example of CBR?) The proof he uses to show that the above is CBV is that fcn_c gets a copy of the pointer 'arrb', thus satisfying the definition of CBV. (essentially correct). But you can extend his argument to all calls that appear to be CBR and prove them to satisfy the parameters for being a CBV. Yet he maintains that C can do CBR, and he rejects my allegation that saying that CBR's exist in C, and *not* calling the above a CBR is inconsistant. (I'm trying to show him that his argument can be used on any 'CBR' to show that it is a CBV). Suggestions/insight? -Sean /*--------------------------------------------------------------------------*\ | Sean C. Malloy = ("x041sc@tamuts.tamu.edu" || "scm3775@tamsun.tamu.edu") | \*--------------------------------------------------------------------------*/ You'll pay to know what you REALLY think. /*--------------------------------------------------------------------------*\ | Sean C. Malloy = ("x041sc@tamuts.tamu.edu" || "scm3775@tamsun.tamu.edu") | \*--------------------------------------------------------------------------*/ You'll pay to know what you REALLY think.
jerry@matt.ksu.edu (Jerry J. Anderson) (03/29/91)
In article <13849@helios.TAMU.EDU> scm3775@tamsun.tamu.edu (Sean Malloy) writes: > Given the following fragment: > > char arrb[] = "mine"; /* not global */ > fcn_c( arrb ); > > ------- > > int fcn_c (char arrb []) > { > arrb[3] = 't'; > } > > Is the above an example of call by value or call by reference? The following is strictly my opinion: Call by value, call by value result and call by reference are strictly compiler attributes. In call by value _the_compiler_ passes a copy of the arguments into the function, but _the_compiler_ does _not_ pass those (possibly changed) parameters back to the original arguments in the calling code. _You_ may change the data the arguments point to, but the compiler doesn't. In call by value result the compiler passes a copy of the arguments to the functions, and when the function is done the compiler passes those (possibly changed) parameters back to the original arguments. In call by reference the compiler does not pass a copy of the arguments; it passes the addresses of those arguments in the calling code. Any changes made to those parameters inside the function are actually changes to the original arguments. So, what happens in C? In C copies of the arguments are passed into the function, but changes to those parameters inside the function are _not_ passed back to change the arguments in the calling code. Therefore, C functions are ALWAYS, ALWAYS, ALWAYS call by value. C was designed to be both a high-level language _and_ to be close to the machine. Indirect addressing at machine level works like this: "do something to the data in the location pointed to by this" This is the way C works, too. Inside a function you can make changes to things outside the function, but you have to have the address of the thing you want to change, and you have to do it yourself. The compiler won't do it for you. That is why C is call by value. But then, this is just my opinion. ;-) -- Newsweek on Gorbachev - "Give back your Jerry J. Anderson Nobel, Comrade Backstabber. Computing Activities P.S. Your tanks stink." Kansas State University Internet: jerry@ksuvm.ksu.edu Manhattan KS 66506