Understanding references
A reference is a type of pointer that is more user-friendly and requires less syntax than a traditional pointer. A few big advantages of using a reference are that you do not have to use the ^
symbol, you do not have to use the ADR
operator, and finally, references are type-safe.
References share many similarities with pointers, including similar syntax. As such, much like pointers, a reference must be declared. Therefore, the first step in learning how to use pointers is to understand how to declare them.
Declaring a reference variable
Declaring a reference is almost the same syntax as declaring a pointer. This can be thought of as a shorthand way of using a pointer. The only difference is that the REFERENCE
keyword is used as opposed to the POINTER
keyword.
This is the syntax to declare a REFERENCE
variable:
<variable> : REFERENCE TO <data type>
Putting this syntax into practice, we can create a reference to an integer, as in...