|
| 1 | +#ByREF |
| 2 | + |
| 3 | +`ByRef` is a modifier used in function parameters to specify that the parameters is passed by reference (that is, |
| 4 | +the original variable is passed to the [FUNCTION](function.md) or [SUB](sub.md)). |
| 5 | + |
| 6 | +##Syntax |
| 7 | + |
| 8 | +It's used in function parameter declaration as in this example: |
| 9 | + |
| 10 | +``` |
| 11 | +FUNCTION plusOne(ByRef a As Ubyte) As UByte |
| 12 | + LET a = a + 1 |
| 13 | + RETURN a |
| 14 | +END FUNCTION |
| 15 | +
|
| 16 | +LET a = 0 |
| 17 | +PRINT plusOne(a): REM prints 1 |
| 18 | +PRINT a: REM prints 1; original value of A has been *modified* within the function |
| 19 | +``` |
| 20 | + |
| 21 | +Here the variable `a` is being modified within the function, and this modification persist upon return. |
| 22 | +Except for arrays, when `ByRef` or [ByVAL](byval.md) is not specified in [FUNCTION](function.md) or [SUB](sub.md) |
| 23 | +parameters, [ByVAL](byval.md) will be used by default. On the other hand, if the parameter is an array, |
| 24 | +and no access is specified, it's supposed to be `ByRef` (arrays cannot be passed by value). |
| 25 | + |
| 26 | +ByRef allows us to pass arrays to [FUNCTION](function.md) or [SUB](sub.md): |
| 27 | + |
| 28 | +``` |
| 29 | +REM arrays passed to functions can be of *any dimensions*. Use LBOUND and UBOUND to detect dimensions! |
| 30 | +FUNCTION maxValue(ByRef a() as Ubyte) As UByte |
| 31 | + DIM i as UInteger |
| 32 | + DIM result As UByte = 0 |
| 33 | + FOR i = 0 TO 4 |
| 34 | + IF a(i) > result THEN result = a(i) |
| 35 | + NEXT i |
| 36 | + RETURN result |
| 37 | +END FUNCTION |
| 38 | +
|
| 39 | +DIM myArray(4) As UByte = {4, 3, 1, 2, 5} |
| 40 | +PRINT "Max value is "; maxValue(myArray) |
| 41 | +``` |
| 42 | + |
| 43 | +In this example, is `ByRef` was omitted, it will be used by default, since the parameter was an array. |
| 44 | +Arrays cannot be passed [ByVAL](byval.md). |
| 45 | + |
| 46 | +`ByRef` is also useful to return values in the parameters. [RETURN](return.md) allows to return a single value |
| 47 | +from a [FUNCTION](function.md), but we can return several values by storing the result in those parameters. |
| 48 | + |
| 49 | +`ByRef` requires the parameter to be an _lvalue_, that is, a variable, an array or an array cell. You cannot use |
| 50 | +`ByRef` with expressions (i.e. numbers) because you cannot store a value in them. |
| 51 | + |
| 52 | +Example of wrong usage: |
| 53 | + |
| 54 | +``` |
| 55 | +FUNCTION plusOne(ByRef a As Ubyte) As UByte |
| 56 | + LET a = a + 1 |
| 57 | + RETURN a |
| 58 | +END FUNCTION |
| 59 | +
|
| 60 | +
|
| 61 | +DIM i as UByte = 7 |
| 62 | +DIM myArray(5) as Ubyte |
| 63 | +
|
| 64 | +PRINT plusOne(5): REM syntax error, 5 is not a variable or an array element. |
| 65 | +PRINT plusOne(i): REM Ok. Will change i value to 8, and print 8) |
| 66 | +PRINT plusOne(i + 1): REM syntax error, i + 1 is not a variable |
| 67 | +PRINT plusOne(myArray(3)): REM Ok. Will increase myArray(3) by 1 and print the result |
| 68 | +``` |
| 69 | + |
| 70 | +See also: |
| 71 | + |
| 72 | +* [FUNCTION](function.md) |
| 73 | +* [RETURN](return.md) |
| 74 | +* [SUB](sub.md) |
| 75 | +* [ByVAL](byval.md) |
0 commit comments