Skip to content

Commit b671504

Browse files
committed
Update documentation
* Add ByREF * Add ByVal * Update Function
1 parent 7e0a0b0 commit b671504

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

docs/byref.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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)

docs/byval.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ByVAL
2+
3+
`ByVal` is a modifier used in function parameters to specify that the parameters is passed by value (that is,
4+
a copy of the value 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(ByVal 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 0; original value of A is preserved
19+
```
20+
21+
Here the variable `a` is being modified within the function, but the original value is preserved upon return.
22+
Except for arrays, when [ByREF](byref.md) or `ByVal` is not specified in [FUNCTION](function.md) or [SUB](sub.md)
23+
parameters, `ByVal` will be used by default. On the other hand, if the parameter is an array, and no access is
24+
specified, it's supposed to be [ByREF](byref.md) (arrays cannot be passed by value).
25+
26+
See also:
27+
28+
* [FUNCTION](function.md)
29+
* [SUB](sub.md)
30+
* [ByREF](byref.md)

docs/function.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ END FUNCTION
7373
If you invoke zxbasic using `-O1` (or higher) optimization flag the compiler will detect and ignore unused functions
7474
(thus saving memory space). It will also issue a warning (perhaps you forgot to call it?),
7575
that can be ignored.
76+
77+
##See Also
78+
79+
* [SUB](sub.md)
80+
* [ASM](asm.md)
81+
* [END](end.md)
82+
* [RETURN](return.md)
83+
* [ByREF](byref.md)
84+
* [ByVAL](byval.md)

0 commit comments

Comments
 (0)