Difference between pass by reference and pass by value?

Showing Answers 1 - 5 of 5 Answers

yogeshpanda

  • Sep 19th, 2005
 

If the base want to retain the ownership of the variables then it passed the variables to the function by referenc, and if it does`t want to retain the ownership of the variables then it passed the variables to the function by value. 

  Was this answer useful?  Yes

yogeshpanda

  • Sep 19th, 2005
 

If the base want to retain the ownership of the variables then it passed the variables to the function by referenc, and if it does`t want to retain the ownership of the variables then it passed the variables to the function by value. 

Silvio

  • Jul 4th, 2008
 

When using by reference there is no new variable just a alias to the memory address, if this parameters is not constant it is possible to change the content of that address.


When using by value a new variable will be created in memory and it is impossible to change the original content.


Everything depend on what you want to do.

There two ways can passes an argument to subroutine (method).
1)call-by-value
2)call-by-reference

call-by-value:
----------------
In this process first method copies the value of an argument in to formal parameters of the subroutine. Therefore any changes made to the parameter of the subroutine will have no effect to argument used to call it.

call-by-reference:
---------------------
In this process method copies the value of an argument in to the formal parameter of the subroutine. Therefore any changes made to the parameter of the subroutine will have effect to the arguments used to call it.

  Was this answer useful?  Yes

Pass By Ref is used Generally, when the Caller wants to retain the ownership of the variable.


Most Imp, Pass By ref is used mostly, when Function Chaining / Assignment Chaining is to be implemented.

With Basic Data type in C, when I say a = b = c = 4; it automatically assign 4 to a, b, c.

But with any of my own object, say of class myClass, like:
class myClass {
...
};

myClass mObj1, mObj2, mObj3;
...
mObj1 = mObj2 = mObj3;
...

Proper assignment will happen only, when assignment operator is implemented with Pass By Ref & Return by Ref.
i.e.
myClass& myClass::operator=(myClass& myObj)
{
...
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions