Table of Contents

BYREF

Parameter or argument modifier to allow "passing by reference"; also used in variable declarations

Implemented by: FreeBASIC, Gambas

With variations:

Also written as:

Usage

In a more common usage, BYREF is used in the context of a function declaration or a function call, so that if a variable name is used as argument, the function is able to access this variable itself, instead of only receiving a copy of the value it holds.

Sub ChangeVar(ByRef AVar As Integer)
    AVar = AVar + 1
End Sub
 
ChangeVar MyVar  ' increments MyVar itself

Variations

In Gambas, a function whose declaration contains a parameter modified by BYREF becomes able to receive the argument as a reference, but it still can come as a value only. The function call must also use BYREF in order to force the argument to be passed by reference:

MyFunc(ByRef outerVar)

FreeBASIC's documentation is more detailed about restrictions and limitations of BYREF and its sister BYVAL (as an example: arrays are always passed by reference, so BYREF is not only unnecessary but forbidden). Also, the -lang directive for "language mode" influences such restrictions and the way parameters are passed by default: in the fb dialect arguments are mostly passed by value, while in qb and fblite dialects arguments are passed by reference by default.

Also, FreeBASIC allows BYREF to be used as a modifier in variable declarations.

Examples

Comments

Similar keywords

In other languages...

BYREF somehow provides a similar effect as that of passing pointers as function arguments in the C language.

References