Syntax

Marks a FUNCTION parameter that receives the caller variable itself instead of a copy, so that the function can change it.

<name> REF AS <type>
<name> BYREF AS <type>

Also written : BYREF

Parts

<name>The parameter name. Write it without a sigil.
<type>The parameter type. Required. The value written through the parameter is converted to it.

Good to know

  • REF and BYREF are the same word. Both go after the name and before AS. The Visual Basic order BYREF n AS integer is refused ("Invalid BASIC parameter"), and so is REF without a type.
  • The argument must be a place you can assign: a variable, a record field, an array element, the current list element or a map entry. A literal or an expression gives "REF argument is not assignable".
  • The type still applies: a REF AS integer that receives a float variable writes an integer into it.
  • A function can pass its own REF parameter on to another REF parameter; the change reaches the first caller.
  • A REF parameter cannot be OPTIONAL and cannot be variadic.
  • Do not put a sigil on it. text$ REF AS string compiles, but the program does not run as soon as the body reads text$. Write text REF AS string.
  • Without REF, an untyped parameter already shares a record or an array with the caller; a typed one gets a copy. To keep a link longer than one call, use a handle (@).

Example

FUNCTION SwapValues(a REF AS integer, b REF AS integer)  LOCAL t AS integer = a  a = b  b = tENDFUNCTIONx AS integer = 1y AS integer = 2SwapValues(x, y)Console(Str(x) + " " + Str(y))   ' 2 1
In the manual : Functions