Page 1 of 1
global and local variables?
Posted: Mon May 30, 2011 1:38 am
by xfrios
I am a livecode beginner. I have good experience in 'C' so technical explanations are fine. I am finding it difficult to navigate the pdf docs and dictionary to find even simple information about syntax and scope.
I have code something like this:
Code: Select all
on pSet
put 6 into termOne
put 4 into termTwo
end pSet
on pVerify
put 10 into x
if x = termOne + termTwo
then
put "Match" into field fResult
end if
end pVerify
The first problem - the values of variables I create in "Set" are not available in "Verify".
The second problem - syntax for the evaluation "if x = termOne + termTwo" is definitely incorrect
Please point me in the right direction.
Thanks!
Re: global and local variables?
Posted: Mon May 30, 2011 2:10 am
by yachris
Hi xfrios,
Welcome! I'm a newby here myself.
In the
userguide.pdf file, section 5.5 "Variables", page 145+, you'll find the variable information. Notably,
makes a variable global and
makes sure it's local.
Interestingly, if you put these inside a function, that makes their scope just that function. If you put them outside any function, then they're local/global for all the functions defined in that script area. The 'global' at script level is obvious, IMHO, but the 'local' at script level is something like declaring a 'static' variable inside a 'C' file; all the functions in that file see it, but no functions outside that file see it.
Re: global and local variables?
Posted: Mon May 30, 2011 4:10 am
by xfrios
Thanks for that, very clear. The LiveCode documentation is so insanely verbose and without any concise summaries like that : ).
I think I'll put some documentation feature requests in.
Gosh, I had an easier time with the MS Win32 API : )
Re: global and local variables?
Posted: Mon May 30, 2011 12:31 pm
by Klaus
Hi xfrios,
xfrios wrote:...Gosh, I had an easier time with the MS Win32 API : )
HA! I strongly doubt this!
Best
Klaus
Re: global and local variables?
Posted: Mon May 30, 2011 1:15 pm
by xfrios
Well um now you mention it, I did have Petzold's api programming book for bedtime reading - maximum information with minimum verbiage.
So yes, I wasn't entirely relying on the documentation in that case now I think about it ; )
Re: global and local variables?
Posted: Mon May 30, 2011 3:56 pm
by bn
Interestingly, if you put these inside a function, that makes their scope just that function. If you put them outside any function, then they're local/global for all the functions defined in that script area. The 'global' at script level is obvious, IMHO, but the 'local' at script level is something like declaring a 'static' variable inside a 'C' file; all the functions in that file see it, but no functions outside that file see it.
to declare a variable inside a handler with local myVar is a way to ensure that it is declared if you use 'Strict Compilation Mode' that you can turn on in the Preferences -> Script Editor. If you don't declare it first and try to use variables on the fly in strict compilation mode the Script Editor will throw an error.
If you do not use the strict compilation mode you don't have to declare a variable inside a handler, it is optional.
If you declare a variable with local sVar outside of a handler the scope of that variable is all handlers inside that script:
This type of variable is referred to as a 'script local variable'. This is different from a local or a global variable.
Code: Select all
local sVar
on something
put "xyz" into sVar
end something
on somethingElse
answer sVar
end somethingElse
as for global variables you have to declare them, but you can declare them inside a handler for use in that handler or outside of all handlers of a script to be accessible for all handlers in that script.
Code: Select all
global gVar
on something
put "xyz" into gVar
end something
on somethingElse
answer gVar
end somethingElse
or
Code: Select all
on something
global gVar
put "xyz" into gVar
end something
on somethingElse
global gVar
answer gVar
end somethingElse
Many people here have adapted the habit to prepend the variables with a letter depending on what type of variables they are:
t (like in temp) for local variables
s (like in script local) for script local variables
g (like in global) for global variables
The beauty of script local variables is that they persist. And that their scope is restricted to the script. Global variables also persist but are accessible from every open stack. That can lead to confusion if you're not careful with the naming etc. I prefer to store stuff that can be accessed from every handler in custom properties.
I hope this helps a little to sort this out.
Kind regards
Bernd
Re: global and local variables?
Posted: Mon May 30, 2011 11:03 pm
by xfrios
Thank you Bernd, nice to see that confirmed & the naming convention.
Re: global and local variables?
Posted: Mon May 30, 2011 11:24 pm
by bn
Hi xfrios,
there is one thing you have to watch out for if you use script local variables.
I always declare them at the top of the script -> no problem.
If you declare them outside of a handler just above a handler that is say the fifth handler of 10 in a script only the handlers below the declaration of the script local variable will have access to the script local variable, not the ones above
Code: Select all
on putSomething
put sVar
end putSomething
local sVar
on loadVar
put "xyz" into sVar
end loadVar
on putSomethingElse
put sVar
end putSomethingElse
you can see this if you put this script inside a card script. Then make three buttons. The first one with loadVar, to fill the script local variable, the second one with putSomething and a third one with putSomethingElse
only the one with putSomethingElse will put xyz into the message box, the one with putSoemething will put sVar into the message box since it does not "see" the script local variable.
As I said if you declare the script locals above all handlers in a script then all handlers see the script local
Kind regards
Bernd
Re: global and local variables?
Posted: Mon May 30, 2011 11:40 pm
by xfrios
Excellent, thank you!
Re: global and local variables?
Posted: Wed Apr 09, 2014 11:26 am
by Mag
Excellent explanation Bernd!
Re: global and local variables?
Posted: Sat Apr 26, 2014 3:36 pm
by MarcVanCauwenberghe
Very nice explanation Brend thank you. Still have trouble with those scopes.
To add a global is not global to the whole program but can be if you add it to every script? Am I saying that correct?
Marc
Re: global and local variables?
Posted: Sat Apr 26, 2014 4:33 pm
by dunbarx
Correct.
Globals must be declared in every script. There is no stack-wide or session-wide declaration. In fact, one might say that a custom property is a global that is session-wide. Do you know about these? I use custom properties far more than globals. We could live without globals; we cannot live without custom properties.
Know also that script local variables may be declared in a script, and that, like globals, must be declared ABOVE the handlers that reference them. This is why it is usual to see these gadgets declared at the top of the script, so all handlers "see" them.
Craig Newman
Re: global and local variables?
Posted: Sat Apr 26, 2014 4:35 pm
by FourthWorld
xfrios wrote:Thanks for that, very clear. The LiveCode documentation is so insanely verbose and without any concise summaries like that : ).
For verbosity, see the User Guide. For conciseness, see the Dictionary.