By value-result vs by reference

ssimmons@convex.com (Steve Simmons)
Thu, 21 Apr 1994 11:57:22 GMT

          From comp.compilers

Related articles
By value-result vs by reference guerin@iro.umontreal.ca (1994-04-21)
Re: By value-result vs by reference graham@pact.srf.ac.uk (1994-04-21)
By value-result vs by reference ssimmons@convex.com (1994-04-21)
Re: By value-result vs by reference hbaker@netcom.com (1994-04-21)
Re: By value-result vs by reference Dik.Winter@cwi.nl (1994-04-21)
Re: By value-result vs by reference nebbe@lglsun.epfl.ch (1994-04-22)
Re: By value-result vs by reference andrewd@apanix.apana.org.au (1994-04-24)
Re: By value-result vs by reference hbaker@netcom.com (1994-04-23)
Re: By value-result vs by reference nebbe@lglsun.epfl.ch (1994-04-26)
[5 later articles]
| List of all articles for this month |
Newsgroups: comp.compilers
From: ssimmons@convex.com (Steve Simmons)
Keywords: design, comment
Organization: CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA
References: 94-04-140
Date: Thu, 21 Apr 1994 11:57:22 GMT

> I would like to hear about call-by-value-result vs call-by-reference,
> which one is better ?


It depends on what your goals are... The semantics of the language
really drive this issue.


> Most of the today popular languages have adopted the call-by-reference as
> a mean to pass variable arguments to procedure-function.


No, that is not true.... C and C++ use call by value although the programmer
can fake call by reference by passing a pointer. FORTRAN is 100%
call by reference. Ada and Pascal permit both.


Here are some quick points on each...


CALL BY REFERENCE


Advantage:


1. A fixed size of parameter is always passed on the stack by the caller.
2. Thus, the caller and callee don't always need to be compiled when passing
something like a structure between the two.
3. Passing things in registers is easier since the pointers can always
be placed in the general purpose registers. Floats and structs make
passing by register more complicated.


Disadvantage:


1. An extra level of dereference.
2. Stores to that variable within the callee cannot be optimized away
since the caller must get the new value.
3. Dynamic arrays that need to maintain the initial value of an array's
bound must retain that value in the prolog of the routine.
4. An actual argument that is either an expression or a constant
must be allocated to memory so that you can pass a pointer to it.


CALL BY VALUE


Advantage:
1. One level of reference to get the value.
2. Assignments to that value can be optimized away within the callee.
3. Expressions and constants must be pushed onto the stack but that is
what your passing.
4. You can have no data memory references within the caller when passing
things in registers.
      int inc(int i) { return i+1; } needs not to touch memory for a parameter.


Disadvantage:


1. Without pointers, you cannot implement call by reference. However,
you can use "call by value"-like semantics with call by reference.
2. A change in data type usually requires both the callee and caller
to recompile.
3. Passing things in registers are faster but can be a bitch when supporting
more than one data type (like floats, structs).
4. Passing an array or struct by value usually requires that you copy
the entire contents of the variable (the whole array or struct) into
a temporary location. This can slow things down.


Thank you.


Steve Simmons
[FYI, for scalar arguments Fortran permits value-return and many Fortran
systems, notably on IBM mainframes, do it that way. C++ is mostly call by
value except for reference parameters. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.