Wednesday, November 19, 2008

“By Value” versus “By Reference"

This subject is discussed in courses and tutorials for many different programming languages. C++ can use either, but Java only uses “By Reference.” “By Value” means that the data is passed to a function, and when the function modifies the data it is not modifying data anywhere else. “By Reference” means a reference to the data is passed to a function, and when a function modifies the data it is modifying the original data store.

The implementation in LabVIEW 8.5 is “By Value.” The object reference actually contains the data. This can be seen because “Unbundle By Name” can be wired to the object reference to directly access the data. See the example below. Also see “LabVIEW Object Oriented Programming: The Decisions Behind the Design” at the following link: http://zone.ni.com/devzone/cda/tut/p/id/3574.


The “By Value” nature of the LabVIEW 8.5 OOP implementation is also shown in the following example:


If this was a “By reference” implementation, the lower “Set Numeric” VI would set the value to 9, but because it is a “By Value” implementation it sets a copy of the data to 9. The value on the upper branch of the object reference wire is not affected. The “Get Numeric” VI returns a value of 5, even though dataflow is controlled so that it executes after the lower “Set Numeric” VI. With the “By Value” implementation whenever a wire branches a copy of the data is created.

With the Single Element Queue Object Oriented Programming, the data is stored in a named queue with a unique ID. The object reference wire does not contain the data, but contains a reference to the queue that stores the data.


In the example above we see that even though the object reference wire is branched to a “Set Voltage” VI that set the voltage value to 9, but the original object reference wire is connected to the “Get Voltage” VI, the value has been set to 9. Branching the object reference wire does not result in a copy of the data. There is only one copy of the data, and it is accessed by a reference to a value. (Also, note that because we are actually using a “By Reference” paradigm, we need to create and destroy the object reference.)




Creative Commons License
LabviewGeek Blog by C. Allen Weekley is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.weekleysystems.com/.

No comments: