Revision [23704]

This is an old revision of ProPgReferences made by fxm on 2019-10-09 23:48:54.


Using References

The syntax for declaring the References and Using them instead of pointers.

The different syntaxes used to declare a reference all use the 'Byref' keyword.
Since a pointer is a variable, it is possible to modify its contents, and the same pointer can allow successive access to different variables. The association between a reference and the object that it designates is, however, fixed when it is declared.

The 'Byref' keyword indicates a variable that is declared by reference. It is used in three different contexts:
- In a procedure signature, to pass an argument by reference (byref parameter).
- In a function signature, to return a variable to the caller by reference (byref return).
- In the body of the code, to define a reference variable (byref variable).

Passing parameter by reference to procedure (byref parameter) Returning variable by reference from function (byref return)
On the left-hand side of an assignment expression using the '=' symbol, the result of the function (returned by reference) must be enclosed in parentheses when the function calls one single argument, in order to solve the parsing ambiguity.
From fbc version 0.90, '=>' can be used for assignments, in place of '=', same as for initializers, allowing to avoid parsing ambiguity (without parentheses):
Declare Function transitbyref( ByRef _s As String ) ByRef As String

Dim As String s

s = "abcd"
Print s

'' the enclosing parentheses are required here.
( transitbyref( s ) ) = transitbyref( s ) & "efgh"
Print s

'' the enclosing parentheses are not required here.
transitbyref( s ) => transitbyref( s ) & "ijkl"
Print s


Function transitbyref( ByRef _s As String ) ByRef As String
    '' This var-len string will transit by reference (input and output), no copy will be created.
    Return _s
End Function

Defining reference variable in code (byref variable) References versus pointers by comparative examples Hacking on usage of references with the additional syntaxes allowed by FreeBASIC
In FB, a reference is implemented under the hood through an internal pointer which holds the address of the variable.

The access to this internal pointer is presently allowed for user, both in read and write (unlike many other languages):
- Therefore, the address of the referred variable (the value of the internal pointer) can be get by using the '@' operator applied on the reference variable symbol name:
variable_address = @ref
- And even, a reference can be reassigned (by modifying the value of the internal pointer) to refer to another variable (of compatible type) by doing:
@ref = @other_variable
- The address of the internal pointer can even be obtained:
internal_pointer_address = @@ref
- A reference can also be re-initialized to a "null" reference:
@ref = 0
- A reference can even be directly declared as a "null" reference:
Thus, by always using the same reference symbol name, one can mix the pure syntax on the reference with the syntax on its internal pointer.

See also
Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode