BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Pass Array to Function"
, by turtle1776
Want to pass arrays to a generic function that deals with arrays? (like a generic sorting function?). Here's how.
Code
;=========================== ;Pass Array to Function Demo ;=========================== ;Unlike types and banks, you can't pass an array to a function using ;normal Blitz commands. But you can do it manually by asigning each ;array a unique ID number and passing that to the function instead. ;The function would then use that ID# in conjunction with readArray() and ;writeArray() commands to read and write to the correct array. ;Dimension the arrays Dim myFirstArray(100) ;array#1 Dim mySecondArray(40,20) ;array #2 Dim myThirdArray(10,4,5) ;array #3 ;Assign some #s to arrays myFirstArray(0) = Rand(100) : myFirstArray(1) = Rand(100) mySecondArray(0,0) = Rand(100) : mySecondArray(1,0) = Rand(100) ;Swap #s in two different arrays using a generic swapArray() function For array = 1 To 2 swapArray(array) Next WaitKey End ;This is an example of a generic function that can be applied ;to any array. This simple function swaps the first and ;second values in the first column of the array Function swapArray(ID) Print "Swapping items in array # " + ID Print "- Before swapping: " + readArray(ID,0) + " " + readArray(ID,1) temp = readArray(ID,0) writeArray(ID,readArray(ID,1),0) writeArray(ID,temp,1) Print "- After swapping: " + readArray(ID,0) + " " + readArray(ID,1) Print End Function ;=========================== ;FUNCTIONS ;This function reads a value from the designated array Function readArray(ID,x,y=0,z=0) If ID = 1 Return myFirstArray(x) Else If ID = 2 Return mySecondArray(x,y) Else If ID = 3 Return myThirdArray(x,y,z) End If End Function ;This function writes a value to the designated array Function writeArray(ID,value,x,y=0,z=0) If ID = 1 myFirstArray(x) = value Else If ID = 2 mySecondArray(x,y) = value Else If ID = 3 myThirdArray(x,y,z) = value End If End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations