Entry
BBCBASIC: Windows: Array: Function: How to pass an array to a function?
Feb 2nd, 2006 17:29
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 02 February 2021 - 11:54 pm -------------------
BBCBASIC: Windows: Array: Function: How to pass an array to a function?
---
There are at least 2 methods possible.
1. -Using string concatenation and the EVAL function
2. -Using the native BBCBASIC for Windows array passing method
===
Conclusion:
The string concatenation method is more general than the other method,
but is certainly much slower, because the string has to be
concatenated
and the EVAL function has to be called to evaluate the array value out
of the string.
===
Method: Using string concatenation and EVAL
Example shows passing a 1 dimensional array:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI% )
:
FOR I% = minI% TO maxI%
READ xR( I% )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( minI%, maxI%, "xR(", ")" )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
LOCAL maxR
LOCAL xR
maxR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set
maximum at first equal to first element
:
FOR I% = minI% TO maxI%
xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43
DATA 2
DATA 789
DATA 9999
DATA 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
===
Example shows passing a 2 dimensional array
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI%, 2 )
:
FOR I% = minI% TO maxI%
READ xR( I%, 1 )
READ xR( I%, 2 )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( minI%, maxI%, "xR(", ", 1 )" )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
LOCAL maxR
LOCAL xR
maxR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set
maximum at first equal to first element
:
FOR I% = minI% TO maxI%
xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43, 43
DATA 2, 2
DATA 789, 789
DATA 9999, 9999
DATA 6, 6
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
===
Example shows passing a 3 dimensional array
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI%, 2, 3 )
:
FOR I% = minI% TO maxI%
READ xR( I%, 1, 1 )
READ xR( I%, 1, 1 )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( minI%, maxI%, "xR(", ", 1,
1 )" )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
LOCAL maxR
LOCAL xR
maxR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set
maximum at first equal to first element
:
FOR I% = minI% TO maxI%
xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43, 43, 43
DATA 2, 2, 2
DATA 789, 789, 789
DATA 9999, 9999, 9999
DATA 6, 6, 6
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
===
Method: Passing the array using the more specific BBCBASIC for Windows
method
(see BBCBASIC for Windows help: search for 'passing')
Example shows passing a 1 dimensional array:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI% )
:
FOR I% = minI% TO maxI%
READ xR( I% )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( xR() )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( A() )
LOCAL maxR
LOCAL xR
LOCAL minI%
LOCAL maxI%
minI% = 1
maxI% = DIM( A(), 1 )
:
maxR = A( minI% ) : REM set maximum at first equal to first element
:
FOR I% = minI% TO maxI%
xR = A( I% )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43
DATA 2
DATA 789
DATA 9999
DATA 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
===
Example shows passing a 2 dimensional array:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI%, 1 )
:
FOR I% = minI% TO maxI%
READ xR( I%, 1 ), xR( I%, 1 )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( xR(), 1 )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( A(), columnI% )
LOCAL maxR
LOCAL xR
LOCAL minI%
LOCAL maxI%
minI% = 1
maxI% = DIM( A(), 1 )
:
maxR = A( minI%, columnI% ) : REM set maximum at first equal to first
element
:
FOR I% = minI% TO maxI%
xR = A( I%, columnI% )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43, 43
DATA 2, 2
DATA 789, 789
DATA 9999, 9999
DATA 6, 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
===
Example shows passing a 3 dimensional array:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI%, 1, 1 )
:
FOR I% = minI% TO maxI%
READ xR( I%, 1, 1 ), xR( I%, 1, 1 ), xR( I%, 1, 1 )
NEXT I%
:
PRINT "maximum = "; FNArrayGetMaximumR( xR(), 1, 1 )
:
END
:
:
:
REM library: determine the maximum
DEF FNArrayGetMaximumR( A(), columnI%, depthI% )
LOCAL maxR
LOCAL xR
LOCAL minI%
LOCAL maxI%
minI% = 1
maxI% = DIM( A(), 1 )
:
maxR = A( minI%, columnI%, depthI% ) : REM set maximum at first equal
to first element
:
FOR I% = minI% TO maxI%
xR = A( I%, columnI%, depthI% )
IF xR > maxR THEN maxR = xR
NEXT I%
= maxR
:
DATA 43, 43, 43
DATA 2, 2, 2
DATA 789, 789, 789
DATA 9999, 9999, 9999
DATA 6, 6, 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
maximum = 9999
---
---
Internet: see also:
---
BBCBASIC: Windows: Array: Minimum: How to determine the minimum value
of an array?
http://www.faqts.com/knowledge_base/view.phtml/aid/39451/fid/768
---
BBCBASIC: Windows: Array: Maximum: How to determine the maximum value
of an array?
http://www.faqts.com/knowledge_base/view.phtml/aid/39447/fid/768
----------------------------------------------------------------------