Reference: Difference between revisions

12 bytes removed ,  15 years ago
m
Syntax
(Clarified, thin/fat pointers, parameter passing fixed, the example (of aliasing) is replaced by a correct one.)
m (Syntax)
Line 7:
The term reference is also used in '''reference semantics''' as opposed to '''value semantics''', especially with regard to the parameter passing convention. An object has reference semantics if it is identifiable through a reference to. In particular, one can compare references in order to decide if they refer to the ''same'' object. When object is not identifiable through references, it is said that it has a value semantics. This implies that the value exhaustively describes the object's state so that two objects having the same value are indistinguishable.
 
Parameters of a subprogram may have either referential or value semantics, in this case one talks about passing by parameters by-reference or by-value. The way of parameter passing is independent on the mutability of parameters. When a mutable (in-out) parameter is passed by value, it is copied in before the call and then copied out after itsupon completionreturn.
 
Referential semantics is exposed to the '''aliasing problem'''. The issue arises when the same object is passed into a subprogram, or becomes otherwise accessible through two distinct names or reference objects. In this case updating the object through one reference would implicitly change the target of another. So in presence of aliasing the program semantics might become dependent on referential or value semantics of the object, thus on an implementation. Example: