Since we now know how to get the address of an array we can simulate passing an array as a procedure parameter by passing the address of the array.
For example, the following program uses a procedure to fill in the first x elements of an array with their index numbers.
DEF a[10]:ARRAY OF INT
PROC main()
DEF i
fillin(a, 10)
FOR i:=0 TO 9
WriteF('a[\d] is \d\n', i, a[i])
ENDFOR
ENDPROC
PROC fillin(ptr:PTR TO INT, x)
DEF i
FOR i:=0 TO x-1
ptr[]:=i
ptr++
ENDFOR
ENDPROC
Here's the output it should generate:
a[0] is 0 a[1] is 1 a[2] is 2 a[3] is 3 a[4] is 4 a[5] is 5 a[6] is 6 a[7] is 7 a[8] is 8 a[9] is 9
The array a only has ten elements so we shouldn't fill in any more than the first ten elements.
Therefore, in the example, the call to the procedure fillin should not have a bigger number than ten as the second parameter.
Also, we could treat ptr more like an array (and not use ++), but in this case using ++ is slightly better since we are assigning to each element in turn.
The alternative definition of fillin (without using ++) is:
PROC fillin2(ptr:PTR TO INT, x)
DEF i
FOR i:=0 TO x-1
ptr[i]:=i
ENDFOR
ENDPROC
Also, yet another version of fillin uses the expression form of ++ in the assignment (see 10.3 Assignments) and the horizontal form of the FOR loop to give a really compact definition.
PROC fillin3(ptr:PTR TO INT, x) DEF i FOR i:=0 TO x-1 DO ptr[]++:=i ENDPROC
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.