Call By Reference

A ParameterPassing mode where a reference (or if you want to be politically incorrect, a pointer) to the actual parameter is passed into the formal parameter; when the callee needs the formal parameter it dereferences the pointer to get it.

Consequences


It seems there is an alternate definition for Call By Reference being proposed. Would the proposer care to expand upon it?

My understanding is that there are only two ways to share values in computer memory, either one copies the value to a new memory location or one one shares the memory location of the value (assembly allows a third option, the sharing of values via registers).

The difficulty associated with making copies of objects is not due to memory use; most objects usually contain few bytes of data. Even strings rarely exceed 100 characters, i.e., are less than one screen line. Very few objects ever exceed 100 bytes in size and many are much smaller. Memory space is typically not a constraint in copying objects; the constraint is the copy algorithm. Copy algorithms are made complex by two issues: embedded objects and derived classes. When creating a copy of an object containing references to other objects, one needs to copy the referenced objects as well, not just copy the reference to the object (the former is known as "deep copying" while the latter is "shallow copying"). Class inheritance poses a greater challenge because when copying a reference to a base class, one needs to know to copy the entire child class. The program does not know the amount of memory space to allocate at compile time, making it extremely difficult to copy the value onto the stack because references to other values on the stack will be affected by the size of the copied object.

If there is an additional method for sharing values besides copying the value and sharing the memory location of the value, I would be interested in hearing it. If not, I think we will have to accept the original definition on this page of Call By Reference being pointer passing with some level of syntax covering it.

Well, there's CallByThunk. And CallByValueResult. CallByThunk is independent of CallByReference or CallByValue, either of those call types can be "thunked." CallByValueResult just appears to by a double copy, it is CallByValue with the return overwriting the original value. The two basic ways of sharing a value between a caller and a called method are still used.


The reason that this page is so confused is that it doesn't distinguish between lvalues and rvalues (LeftValues and RightValues in wiki syntax).



(text moved from PassByReference:)

There are two basic choices when passing parameters to a function / method:

If you have a parameter passed by reference, changes to it are reflected in the caller's value. Therefore your function can return values through its parameters.

CeePlusPlus example:

 void f( int x )
 {
   x = x * 2;
 }
Calling function f on an integer variable will leave the value unchanged. It is essentially redundant. However,

 void g( int& x )
 {
   x = x * 2;
 }
calling function g on an integer variable will double the value. CeeLanguage can approximate this behaviour through the use of pointers:

 void h( int* x )
 {
   (*x) = (*x) * 2;
 }
calling function h on a pointer to an integer variable will double the value.


"ByRef" in VisualBasic.

"ref Blah b" in C#.



See ParameterPassing, CallByValue, ReturnByReference


CategoryCpp CategoryLanguageFeature


EditText of this page (last edited August 22, 2010) or FindPage with title or text search