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 / GLOBAL and LOCAL keywords?

Author
Message
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 26th Sep 2011 23:06
Hi all!

I'm trying to figure out exactly... how are the GLOBAL and LOCAL keywords used?
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 26th Sep 2011 23:45 Edited at: 26th Sep 2011 23:45
"Global" variables are placed at the top of your source code and define that the variable can be accessed absolutely anywhere:



Here, a global variable is used and altered within a function, so basically "global" lets you access a variable from anywhere in your proigram, and can be a great way of sharing data between several functions. Arrays are defined as global by default, by the way.


Now lets look at what happens if you just remove the word "global" from the above snippet:

The code runs, and prints 5 - but why? The function also uses a g_Temp variable, but it is not the same g_temp declared at the top. Within a function, all variables are "local" by default, or unique to within the function. The function will use local variables by default, if a global or a local variable with the same name exists.

So, you can declare variables within a function as "local" just to make sure that you don't alter a "global" variable by mistake.

Hope this helps!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 27th Sep 2011 01:55
Thanks! That helps immensely! The scope of it all was confusing.

Login to post a reply

Server time is: 2024-11-24 01:58:56
Your offset time is: 2024-11-24 01:58:56