Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / how to pass whole arrays in to functions?

Author
Message
Franky MCLanky
20
Years of Service
User Offline
Joined: 17th Jun 2004
Location:
Posted: 20th Jun 2004 20:17
any quick way of doing this, how would you guys do it?
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 20th Jun 2004 20:43
This is a known issue.


Arrays are global by default. Just define them outside of any function. It's messy but we have to deal with it. I can't remember if this is going to be fixed anytime soon.

Another solution is to use memblocks and pointers.

~zen


Franky MCLanky
20
Years of Service
User Offline
Joined: 17th Jun 2004
Location:
Posted: 20th Jun 2004 21:21
i see.

could you give me an example of how you would pass an entire , two dimensional array into a function that deals with them. i know i have to define them outside the function, but i dont know a way to pass them all in.
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 20th Jun 2004 21:35
just use it! eg:

sync on:sync rate 40
set text opaque
dim a(10)
fill_array()
ac=1
do
for i=1 to 10
text ac,i*20,str$(a(i))
next i
ac=ac+1
if ac>300 then ac=1
sync
loop


function fill_array()
for i=1 to 10
a(i)=rnd(200)
next i
endfunction

allows you to fill the array inside a function, and then uses the array in the main loop, like wise the array could have already had values in it, they could then be modified inside the function eg:

sync on:sync rate 40
set text opaque
dim a(10)
rem fills the array with values
for i=1 to 10
a(i)=rnd(200)
next i
add_one_thousand()
ac=1
do
for i=1 to 10
text ac,i*20,str$(a(i))
next i
ac=ac+1
if ac>300 then ac=1
sync
loop


function add_one_thousand()
for i=1 to 10
a(i)=a(i)+1000
next i
endfunction

creates some values outside the function, fills the array, then calls the function to add one thousand to the values, then uses the result in the main loop, arrays are global, I don`t have an issue with it but the OOP bunnies get all excited

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster.
Franky MCLanky
20
Years of Service
User Offline
Joined: 17th Jun 2004
Location:
Posted: 21st Jun 2004 00:05
ahh thanks, i didnt know that, but what i mean is , say if i want to PASS IN an array, in the parameters, so that i am not restricted to a specific array and i will obviously be needing to pass in the dimensions as well.

e.g. FUNCTION somefunction-
-(arrayname$,firstdimensionrange,seconddimensionrange)
Franky MCLanky
20
Years of Service
User Offline
Joined: 17th Jun 2004
Location:
Posted: 21st Jun 2004 00:06
can this be done , and if it can what is the quickest way to do it?
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 21st Jun 2004 00:37
Franky MCLanky,

As of right now you cannot pass an array to a function as a parameter.

--------------------

If you dim your arrays at the beginning of your code (outside of any function), any function & any other part of the program can use, any of the arrays. You are not restricted to any one array.

All of your arrays if dimensioned outside of a function are global to the entire program. Then, if you change an element of the array inside a function it will effect the global array.
--------------------

If you declare/or dim an array inside a function. Only that function will be able to make changes to the array. There is no way to pass-in or pass-out the entire array to the rest of the program.

You can pass specific values in and out. but you can't pass the entire array
--------------------


In summary, for now, Just define all of your arrays that need to be accessed at the beginning of your program & outside of any particular function. All of your arrays can then be manipulated by any part of your code.


Franky MCLanky
20
Years of Service
User Offline
Joined: 17th Jun 2004
Location:
Posted: 21st Jun 2004 11:09
thanx for clearing that up!

Login to post a reply

Server time is: 2024-11-26 22:38:56
Your offset time is: 2024-11-26 22:38:56