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 / Trying my luck again

Author
Message
Briere
20
Years of Service
User Offline
Joined: 28th Feb 2005
Location: Amherst New York, United States
Posted: 10th May 2006 03:13
Its been over a year since I 1st started using dark basic pro, I used it for a few months, then bought the DGSDK, and Id figure id give DBP some use now, its been sitting in my hard drive collecting e-dust

I forgot, functions in darkbasic... can they change the values of global variables?

example:

iA = 0

FUNCTION test(iA)
iA = 1
ENDFUNCTION

Will that make iA = to one if I print it outside of that function?
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 10th May 2006 14:45
nope, outside the function iA is 0 - This is the lassic board, not the DB Pro board, but in this case the answer is the same
Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 10th May 2006 15:32
The only variables in DBC that are global, are arrays.

dim iA(0)
iA(0) = 0

function test()
iA(0) = 1
endfunction

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 11th May 2006 21:42
really? so:

dim iA(0)
iA(0) = 0
test()
print iA(0)

function test()
iA(0) = 1
endfunction

Would iA(0) print as 1? I thought it would be zero. Based on my previous knowledge, I yesterday, re-wrote a whole chunk of one of my projects; converting functions like the one above in to subs. When it now sounds like it wasn't required
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 13th May 2006 03:09
Arrays are global in DBC if declared outside of functions. Their 'scope' is therefore the whole program.

Arrays defined inside a function are local and their scope is just that function.

In DBC, arrays are used declared outside functions because of the availability of official global variable types.

SimSmall's example would indeed print 1.

However, change it to:

test()
print iA(0)
wait key

function test()
dim iA(0)
iA(0) = 1
endfunction

and the program would error because iA(0) does not exist outside of the function.

TDK_Man

Uncle Sam
19
Years of Service
User Offline
Joined: 23rd Jul 2005
Location: West Coast, USA
Posted: 13th May 2006 03:36
Really though, I think you could easily test this on your own:





Uncle Sam
Nvidia Geforce 7600 GS 256MB PCIEx, 2.66 GHZ Pentium 4 proccessor, 768MB RAM
Need particles? Click here!
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 13th May 2006 13:40
Well, the help page is what really confused me, take a look at the screenshot. As I read it, it tries to make out that everyone here is wrong about this:

Attachments

Login to view attachments
Bahamut
19
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Brighton
Posted: 13th May 2006 16:30
Hi,

I really haven't been programming for nearly as long as my join date suggests, but I'm pretty sure that "global variables" are slightly different to arrays.

In TDK's tutorials, he makes the distinction between normal variables and arrays. Normal variables declared outside a function (in this case iA) are (in his words) "semi-global", which is to mean that they cannot be called inside fuctions. Arrays, however, are completely global and can be called inside functions.

I'm pretty sure that the DB help files are refering to normal variables only.

http://www.myspace.com/Icebound -No, really-We're quite good.
MartinS
19
Years of Service
User Offline
Joined: 15th Dec 2005
Location: Rochester, NY
Posted: 13th May 2006 21:25
Just make your variable global, like this:


Hope that helps,
Martin

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 14th May 2006 04:20 Edited at: 14th May 2006 05:07
Quote: "I'm pretty sure that "global variables" are slightly different to arrays"


You are correct they are. Numeric and string variables are single entities. Single dimensioned arrays are long 'chains' of these variables in a line - with Dim() used to state in the brackets how many links you want in the 'chain'.

The confusion has arisen because this is the Dark Basic Classic board - notice where you are Games 2 Live? and akzel's original post asked about Global Variables.

In DB Classic, they do NOT exist - only in DB Pro.

Sven B correctly stated that to get around this limitation, in DBC you can use arrays as global variables if you need them.

Just do a Dim VariableName(0) and then use VariableName(0) instead of just 'VariableName' and as long as the Dim is outside your functions, they can see and alter the contents.

Any arrays dimensioned inside a function cannot be seen outside that function.

SimSmall

The DBC help files aren't brilliant and do contain errors. However, the section of the help you showed in your screenshot is wrong - but not the exact section you highlighted. It actually says:

Quote: "Any variables declared in the function are what are known as local variables. Local variables can only be recognized and used from within the function."


Correct. Use Dim MyVar(0) in Function X then it is local to that function and can't be seen outside it.

Quote: "Global variables on the other hand are variables declared outside of any function."


Here it's referring to global as being available to the rest of the main program - not inside functions. It is confusing because the term 'global' has been used whereas I would have used the term 'normal'.

Quote: "Your function cannot recognize or use global variables."


Again, replace 'global' with 'normal' and it reads a lot better.

Quote: "The same rules apply to arrays and parameters passed into the function. Parameters declared by the function can be used in the same way you would use any local variable, in order to bring information from the outside world."


And this is where the real error is. This paragraph refers to arrays and variables passed to a function in the parameter list as being local. In fact, you can't pass an array to a function in that way - only normal variables.

The fact is that arrays dimensioned outside of a function shouldn't actually be global either and the fact that it works is a convenient bug - but a bug none-the-less.

There's also another little-known bug in DBC's functions which I always found quite convenient - non-destructive local variables.

When a variable is declared in a function, on exit from that function, all local variables are destroyed. When you use the function the next time, you start again from scratch with all local variables set to 0 (zero) or in the case of strings, Null ("").

Well, that's how it's supposed to be. In fact, DB doesn't destroy the local variables, so the next time you call the function, all the variables are still set to the value they were last time.

An example:



In this example, when you press a key to call the function a second time, A should be zero, but as it wasn't destroyed the last time the function was exited, it's still 100.

This to me can be very useful, but worth bearing in mind if you trying to track down bug and assume that DB's functions work like other languages.

TDK_Man

Login to post a reply

Server time is: 2025-05-25 02:01:43
Your offset time is: 2025-05-25 02:01:43