[futurebasic] Arrays As Parameters [FBII,FB^3]

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2000 : Group Archive : Group : All Groups

From: scram@...
Date: Thu, 1 Jun 2000 07:34:10 -0600
Recent letters have asked how to use an array as a parameter of a function.
Here's what I've done and it seems to work successfully.

a) In the function definition, define the array parameter with (1) after it
to indicate an array. It's convenient to have another parameter indicating
how many items are in the array too.
    ex.  LOCAL FN sortArray( PP(1), n )
	   ...
	 END FN

b) In the calling routine, use the arary's name and subscript zero (0) to
indicate its location in memory. use the second parameter to give the size
of the array.
    ex. FN SortArray(  AA(0), 10 )
    This uses actual array called AA with ten items.

Any changes made within the function to aray items PP(x) will be actually
done to array items AA(x). There's no need for global variables (although
that's another approach) and the same function can now work with different
arrays - a big bonus.

The sample below illustrates this. Two arrays AA(10) and BB(20) are created
and filled with random numbers. Each is printed, sorted and printed again,
using the same same local functions for printing and sorting. This works in
both FBII and FB^3.

PS - Some modifications are needed when using multi-dimension arrays.
E-mail me if interested.

- HTH  - Stu Cram, Regina, SK, CANADA

======================================
' Demo of Passing Arrays To local Functions...

DIM AA(10), BB(20)

'*********************************************
LOCAL FN sortArray( PP(1), n )
 FOR a = 1 TO n-1
  FOR b = a+1 TO n
   LONG IF PP(a) > PP(b)
    SWAP PP(a), PP(b)
   END IF
  NEXT b
 NEXT a
END FN
'*********************************************
LOCAL FN printArray( PP(1), n, title$ )
 TEXT _Courier, 12, 0
 PRINT
 PRINT title$
 FOR x = 1 TO n
  PRINT USING "####"; PP(x);
  NEXT x
 PRINT
END FN
'*********************************************

' Main program....

WINDOW 1, "passing Arrays test", (0,0)-(700,500), 5
TEXT _Geneva, 12
FOR x = 1 TO 10
 AA(x) = RND(100)
 PRINT AA(x);
NEXT x
PRINT

FN printArray( AA(0), 10, "Array AA() - Original" )
FN sortArray( AA(0), 10 )
FN printArray( AA(0), 10, "Array AA() - After Sorting" )

PRINT : PRINT : PRINT

TEXT _Geneva, 12
FOR x = 1 TO 20
 BB(x) = RND(100)
 PRINT BB(x);
NEXT x
PRINT

FN printArray( BB(0), 20, "Array BB() - Original" )
FN sortArray( BB(0), 20 )
FN printArray( BB(0), 20, "Array BB() - After Sorting" )

PRINT
INPUT a$
'=====================