Still confused about local and global

I am still confused about local and global.

I thought local variables and function where only accessible in the block that they are declared. So why does something like this work?

[code]
local disappear = function ()
mo.alpha = 0
end

local appear = function ()
–Take .3 seconds to appear
transition.to(mo, {time = 300,alpha = 1})
–Disappear after .5 seconds
timer.performWithDelay(moDisappearTime, disappear, 1)
end

[code] [import]uid: 8192 topic_id: 1819 reply_id: 301819[/import]

locals are valid until the end of the block in which they are declared.

for example disappear is valid until the end of the file.
also dont forget that

local disappear = function() end

is the same as

local function disappear() end

[import]uid: 846 topic_id: 1819 reply_id: 5635[/import]

There is a slight difference between the two function defines.

local x1 = function()  
 print( "local disappear: " .. tostring(x1) ) -- prints "nil"  
end  
  
local function x2()  
 print( "local disappear: " .. tostring(x2) ) -- prints the function address of x2  
end  
  
x1()  
x2()  

If your function is going to call itself (recursion) you need to use the second form (x2) so the function name is defined first. In the first form, x1 isn’t define until the end of the function.

Printing out a variable, object, or function using “tostring” is a good debug tool to verify that it’s within the scope of your code. If it prints 'nil" that generally means it hasn’t been defined or not accessible.

-Tom [import]uid: 7559 topic_id: 1819 reply_id: 5699[/import]

In short - you should try not to use

local x = function() end

and instead prefer

local function x() end
[import]uid: 846 topic_id: 1819 reply_id: 5911[/import]

Dear akhtar,
Could you plz tell me why?

/*
In short - you should try not to use

local x = function() end

and instead prefer

local function x() end
*/

[import]uid: 9190 topic_id: 1819 reply_id: 6506[/import]