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.

DarkBASIC Discussion / arrays and functions

Author
Message
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Mar 2009 04:43
is there any way to bypass the rule that you cant access global arrays in a function?
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Mar 2009 04:48
now im confused, why am i getting an error in a function about array accessed out of bounds, i thot that only happened if u called an array dimmensionalized outside of the function?

this array was dimmed inside the function, so why am i getting this error?
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 25th Mar 2009 05:18
Quote: "is there any way to bypass the rule that you cant access global arrays in a function? "

There is no such rule, unless I misunderstand what you are trying to achieve.

That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Mar 2009 05:23
i keep getting an error saying the array was accessed outside of bounds wenever i try to use it in a function, why?
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 25th Mar 2009 07:04 Edited at: 25th Mar 2009 07:04
It means the array index you used was higher than the amount of elements in the array. How about posting some code and explaining what you expect it to do?

BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 25th Mar 2009 08:07
For the globals, there ARE no globals in darkbasic, though arrays can be used as such. Just make sure that the array is declared outside of the function and you should be fine (though I don't know for certain how it would work if it is declared within the function).

For your error, Benjamin is right, it means that if you declared your array like this:

dim array(5)


you would get that if you tried to reference slot 6 or 10 or 1000 or any number greater than 5.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
feiting shadow
19
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 25th Mar 2009 09:18
variable scope would be handy to know in this case...
b as integer

do
`in this area, b is recognized, but a isn't
loop

function func1()
a as integer
`when a variable is declared in a function, it can only be used in that function
endfunction

function func2()
a as integer
`this is a completely different variable. If I wrote print STR$(a) instead, it would give me an error. the other a disappears as soon as its function goes away.
endfunction

The basic thing to remember is, whenever a variable is declared, it can be used anytime after it's been declared. But functions are like little bubbles with their own sets variables. This is why I set every variable at the top (using global var as integer as my syntax) except temporary ones like for/next counters. Hope that helps with future issues as well as this one.

Signed
------
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 25th Mar 2009 11:48
Good stuff shadow. Just throwing this into it though: there are no variable declarations of any kind in DBC (except arrays of course).

Also, an interesting (and ever so annoying as well) fact about variables in functions: they retain their value within the function, so:



Will output:
0
1

because the variable a retains its value within the function even after the function is over. Why this can be a problem: any flags you use in a function will STILL BE SET when you call it again, so it will skip through things. The solution: at the beginning of your functions "declare" all the variables used to 0 (so just add a=0 or whatever to the beginning).

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Mar 2009 14:44 Edited at: 25th Mar 2009 16:26
oh, interesting stuff, so ill go ahead and post the code and images in a zip

[edit]
just realized ull prolly have to change the directory for the load images, i have all of it on a flashdrive so thats where i have the commands set to
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 26th Mar 2009 04:49
oh and you can't dim arrays in functions(well i can't i tried but they never work)

New Site! Check it out \/
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 26th Mar 2009 07:25
I have gotten it to work, but they have to already be declared (otherwise the program won't be able to find the declaration and will yell at you for undeclared arrays). So you can RE-dim arrays to a useful size.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 26th Mar 2009 14:09
arrays declared at the begining of the program are global and can be seen by all functions.

variables and arrays declared inside a function can only be seen by the function using them

All variables by default are integers unless you declare them as a string using the $ after them. so you dont have to declare them As integer.

if you need an array to be local and accessable only to a particular function simply declare one inside the function but make sure you use UNdim before exiting the function to release the resources used.

out of bounds error :
BN2 Productions covered this nicely.

additional array notes :
arrays in dbc are zero based making the last element in any given array null. this means you should never assign a value to the last element otherwise the data will for the entire array will become corrupted so if you need:
somearray(3,3,3) or anotherarray(5)
then declare :
somearray(4,4,4) / anotherarray(6)

since arrrays in dbc are zero based, it also means that the first element in any given array is 0 so you Can do this :


dynamic arrays is not a strong point of dbc, you can not dim an array and then resize them on the fly without problems but you can write a small snip of code that can get the size that you will need to use before declare them [plan carefully ]

functions remember values :
always assign a default value for any variables at the begining of function unless you want it use the value that was prevously there as old values do stay in memory



Quote: "oh and you can't dim arrays in functions(well i can't i tried but they never work"


caleb, I have never had this problem except when I used an older version. Are you using an the old cd version ?

bluestar4~
---Missle Might - Hero Battles - Zillipede --- which do you like the best ?
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 26th Mar 2009 16:20
hmm, interesting bluestar, ive never had a problem with using the last element of an array
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Mar 2009 17:14
Have you tried UNDIM the array, since you are dimensioning it inside the function DB would try to dim it every time you call the function, and im pretty sure that would give an error.

Why do you need to dim the array inside a function anyway, that seems an odd thing to do.

I should have read bluestars post
@bluestar
are you sure you can't use the last element of an array? I haven't had any problems.

The Universe has been erased by a mod because it was larger
than 240x80 pixels.
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 26th Mar 2009 18:35
I'm pretty sure that for most forms of BASIC you can access the last element of an array (a 10 element array is thus in fact 11 elements). It's different in C++ where the last array element is n-1.

That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 26th Mar 2009 20:49
btw i think ive got my problem solved, the error wasnt bcuz of using an array in a function, like i initially thought. I just had a minor math error that caused an if command to ask for a negative element of an array, but like i said its fixed now
Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 26th Mar 2009 22:38
Quote: "Why do you need to dim the array inside a function anyway, that seems an odd thing to do."

there are many occassions when you might want to place an array locally. One such occassion would be to keep track of bullets flying around (positions) and how much life they have left before they dissappear. since variables inside functions by default remember their last assignment , this can easily be implemented inside a sub-routine or function. one of the tutorials demonstrates this quite accurately when talking about "bullet life"
Quote: "are you sure you can't use the last element of an array? I haven't had any problems"

unless they updated that part of the compiler somewhere to automatically add a null (which they may have done and I missed reading it somewhere) it will do as I said. dont get me wrong, you can access and assign datat to it without the compiler throwing any warnings whatsoever(just like in c+) because its not out of bounds, however (again unless they changed it somewhere) once done corrupts the prevous data stored into it. your welcome to test it and let me know if its been changed and if I'm wrong and I'll happily correct myself and update my personal reference to include that info. but as far as I am aware this is correct of versions .7 to 1.10
Either way its always a good idea to leave extra room for a growing program because what if you decide to update the program ? A major pain in the arse is that previously saved games would'nt be useable in the newer version if you added more elements to one of the arrays in the program. So, as a general rule I perosnally always add atleast 50 extra spaces in single arrays to ensure future compatibility incase I want to add new features later on down the road the arrays will already be there and neatly coded so and This gives me plenty of headroom to develope the program further without making major changes.

bluestar4~
---Missle Might - Hero Battles - Zillipede --- which do you like the best ?
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Mar 2009 03:09
Quote: "I'm pretty sure that for most forms of BASIC you can access the last element of an array (a 10 element array is thus in fact 11 elements). It's different in C++ where the last array element is n-1."

I agree with Benjamin, though it can't hurt to err on the side of caution. I think even in the manual, there is an example of lottery numbers being stored in a 52 element array for 52 weeks where 1 to 52 indexing is used.

Enjoy your day.
Bluestar4
20
Years of Service
User Offline
Joined: 19th Dec 2005
Location: USA
Posted: 28th Mar 2009 19:09
hey latch , yes your correct, it is printed in the manual, but the manual has had some small misprints in it if you recall

bluestar4~
---Missle Might - Hero Battles - Zillipede --- which do you like the best ?

Login to post a reply

Server time is: 2026-07-21 21:24:04
Your offset time is: 2026-07-21 21:24:04