Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between call by reference and call by value [duplicate]

Tags:

c++

c

Possible Duplicate:
Difference between value parameter and reference parameter ?

What is the difference between call by reference and call by value?

like image 953
swapnadip Avatar asked Feb 17 '10 06:02

swapnadip


People also ask

What is the difference between call by reference and call by value?

In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

What is the difference between call by reference and call by address?

Call By Address is a way of calling a function in which the address of the actual arguments is copied to the formal parameters. But, call by reference is a method of passing arguments to a function by copying the reference of an argument into the formal parameter.

What is meant by call by reference?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Is call by reference better than call by value?

Call by Reference- Actual values undergo the same fate as the formal parameters do. Call by Value uses extra space for formal parameters and making Call by Reference more memory efficient. Since no copies are being made in Call by Reference, it is faster than Call by Value.


1 Answers

In C, there is no call by reference. The closest you can get is taking an address, and passing a copy of that address (by value -- see below).

In C++, call by reference passes a reference to an object (an alias for the original object). Generally this will be implemented as the object's address, though that's not guaranteed.

Call by value means taking a value of some sort, and passing a copy of that value to the function.

The basic difference is that when you pass a parameter by value, the function receives only a copy of the original object, so it can't do anything to affect the original object. With pass by reference, it gets a reference to the original object, so it has access to the original object, not a copy of it -- unless it's a const reference, it can modify the original object (for one example).

like image 136
Jerry Coffin Avatar answered Sep 25 '22 12:09

Jerry Coffin