Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!m.cs.uiuc.edu!ux1.cso.uiuc.edu!maverick.ksu.ksu.edu!matt.ksu.edu!jerry From: jerry@matt.ksu.edu (Jerry J. Anderson) Newsgroups: comp.lang.c Subject: Re: Call by [Value, Reference] V1.1 Summary: C is _always_ call by value Message-ID: <1991Mar29.015541.11401@maverick.ksu.ksu.edu> Date: 29 Mar 91 01:55:41 GMT References: <13849@helios.TAMU.EDU> Sender: news@maverick.ksu.ksu.edu (The News Guru) Distribution: na Organization: Kansas State University Lines: 57 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