Call by [Value, Reference] V1.1

Jerry J. Anderson jerry at matt.ksu.edu
Fri Mar 29 11:55:41 AEST 1991


In article <13849 at helios.TAMU.EDU> scm3775 at 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 at ksuvm.ksu.edu                     Manhattan  KS  66506



More information about the Comp.lang.c mailing list