Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by Reference / Value in C++

I would like to clarify the differences between by value and by reference.

I drew a picture

enter image description here

So, for passing by value,

a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy

How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference "

Thanks!

like image 354
user36064 Avatar asked Jan 04 '09 07:01

user36064


People also ask

Does C use pass by value or reference?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.

What is pass-by-reference example?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

What does it mean to pass a value by reference?

Pass by reference. Passing by reference means that the memory address of the variable (a pointer to the memory location) is passed to the function. This is unlike passing by value, where the value of a variable is passed on. In the examples, the memory address of myAge is 106.

Why is C pass by value?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.


1 Answers

I think much confusion is generated by not communicating what is meant by passed by reference. When some people say pass by reference they usually mean not the argument itself, but rather the object being referenced. Some other say that pass by reference means that the object can't be changed in the callee. Example:

struct Object {     int i; };  void sample(Object* o) { // 1     o->i++; }  void sample(Object const& o) { // 2     // nothing useful here :) }  void sample(Object & o) { // 3     o.i++; }  void sample1(Object o) { // 4     o.i++; }  int main() {     Object obj = { 10 };     Object const obj_c = { 10 };      sample(&obj); // calls 1     sample(obj) // calls 3     sample(obj_c); // calls 2     sample1(obj); // calls 4 } 

Some people would claim that 1 and 3 are pass by reference, while 2 would be pass by value. Another group of people say all but the last is pass by reference, because the object itself is not copied.

I would like to draw a definition of that here what i claim to be pass by reference. A general overview over it can be found here: Difference between pass by reference and pass by value. The first and last are pass by value, and the middle two are pass by reference:

    sample(&obj);        // yields a `Object*`. Passes a *pointer* to the object by value.         // The caller can change the pointer (the parameter), but that         // won't change the temporary pointer created on the call side (the argument).       sample(obj)        // passes the object by *reference*. It denotes the object itself. The callee        // has got a reference parameter.      sample(obj_c);        // also passes *by reference*. the reference parameter references the        // same object like the argument expression.       sample1(obj);        // pass by value. The parameter object denotes a different object than the         // one passed in. 

I vote for the following definition:

An argument (1.3.1) is passed by reference if and only if the corresponding parameter of the function that's called has reference type and the reference parameter binds directly to the argument expression (8.5.3/4). In all other cases, we have to do with pass by value.

That means that the following is pass by value:

void f1(Object const& o); f1(Object()); // 1  void f2(int const& i); f2(42); // 2  void f3(Object o); f3(Object());     // 3 Object o1; f3(o1); // 4  void f4(Object *o); Object o1; f4(&o1); // 5 

1 is pass by value, because it's not directly bound. The implementation may copy the temporary and then bind that temporary to the reference. 2 is pass by value, because the implementation initializes a temporary of the literal and then binds to the reference. 3 is pass by value, because the parameter has not reference type. 4 is pass by value for the same reason. 5 is pass by value because the parameter has not got reference type. The following cases are pass by reference (by the rules of 8.5.3/4 and others):

void f1(Object *& op); Object a; Object *op1 = &a; f1(op1); // 1  void f2(Object const& op); Object b; f2(b); // 2  struct A { }; struct B { operator A&() { static A a; return a; } }; void f3(A &); B b; f3(b); // passes the static a by reference 
like image 149
Johannes Schaub - litb Avatar answered Sep 30 '22 17:09

Johannes Schaub - litb