Wednesday, August 18, 2010

What does it mean to pass data by value and how does that compare to passing data by reference?

What does it mean to pass data by value and how does that compare to passing data by reference? Explain to me why we would pass some data by value and others by reference.


What does it mean to pass data by value and how does that compare to passing data by reference?
if you pass data by value then you are only passing the value stored in that variable. If you then change that value, in a method for example, it doesn't change the value in the original variable.





On the other hand if you pass by reference, and you change the value, then it WILL change in the original variable.





There will be times, such as when you work with arrays, that you might want to use the value stored in a particular spot in the array and use it in a calculation. Other times you will want to change the actual value in the array itself.








hope that helps some.What does it mean to pass data by value and how does that compare to passing data by reference?
If you pass data by value, you are sending the data itself. By reference you are sending the address of a memory location where the data is. In non-computer terms, I can tell you Columbus' sailed in 1492, or I can tell you that data on Columbus first voyage is at www.yahoo.com/columbus. Why would you do these? You would pass data generally if you created the value (as by calculation or lookup) or if the data is absolute like a constant. you would pass it by reference if there are other processes writing data to that memory, for example if you want your program to read whatever the keyboard took in during a wait cycle.

No comments:

Post a Comment