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.

Code Snippets / [DBP] - Showing how variables exist seperately within and outside functions

Author
Message
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 31st May 2012 12:47 Edited at: 31st May 2012 12:52
While studying Javascript and Query I came across variables within and outside functions, I have both Hands on Dark Basic Pro books and recall something similar and thought hey lets give this a go...

So I am sharing this in case anybody has a need for such a concoction...

the rest of this post is inside the code in comments...



This is my first code snippet and I hope to post more in the near future but I hope this helps others understand the fundamentals of Variables

EDIT

Fixed a typo in a comment

EDIT

I always misspell separate, can a mod please if they come across this change the spelling in the title... thanks.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 2nd Jun 2012 19:14
To elaborate, this is called the scope of a variable. Variables are either global or local. Global variables have separate memory that exists throughout the runtime of the program, and when you read or change that variable, you're reading or writing to that space in memory. Local variables have only a temporary lifetime and are only accessible within functions. When you call a function, it creates what's called a stack frame or activation record, with memory set aside for your local variables. When the function ends, the memory is freed and the local variables can't be accessed again.

Worth knowing.

"everyone forgets a semi-colon sometimes." - Phaelax
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 2nd Jun 2012 19:51
Thanks Zotoaster I was tired when I wrote the post

That sums it up precisely

Login to post a reply

Server time is: 2024-04-20 06:40:52
Your offset time is: 2024-04-20 06:40:52