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: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 is 0 a is 1 a is 2 a is 3 a is 4 a is 5 a is 6 a is 7 a is 8 a is 9
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
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.