Page 1 of 1
Difference local function and function
Posted: Wednesday 10 June 2020 19:52
by Bumke1963
Can someone explain when to use a local function and when a 'normal/non-local' function ?
And maybe you are the right person also who can explain a local variable and how to use a global variable ?
Re: Difference local function and function
Posted: Wednesday 10 June 2020 20:42
by waaren
Bumke1963 wrote: ↑Wednesday 10 June 2020 19:52
Can someone explain when to use a local function and when a 'normal/non-local' function ?
And maybe you are the right person also who can explain a local variable and how to use a global variable ?
There are books written about this question and these are freely available on the internet. If you don't have time to study these, the rule of thumb is to use local everywhere for functions and variables where possible. It limits the scope of them (less hard to find bugs) and in Lua it has also significant performance advantages.
ref
Re: Difference local function and function
Posted: Wednesday 10 June 2020 20:51
by Bumke1963
waaren wrote: ↑Wednesday 10 June 2020 20:42
Bumke1963 wrote: ↑Wednesday 10 June 2020 19:52
Can someone explain when to use a local function and when a 'normal/non-local' function ?
And maybe you are the right person also who can explain a local variable and how to use a global variable ?
There are books written about this question and these are freely available on the internet. If you don't have time to study these, the rule of thumb is to use local everywhere for functions and variables where possible. It limits the scope of them (less hard to find bugs) and in Lua it has also significant performance advantages.
ref
I google'd a lot on this subject, didn't find one LUA example in combination with Domoticz which explains the difference.
Someone has a better idea or a link to an example ?
Re: Difference local function and function
Posted: Wednesday 10 June 2020 22:11
by waaren
Bumke1963 wrote: ↑Wednesday 10 June 2020 20:51
I google'd a lot on this subject, didn't find one LUA example in combination with Domoticz which explains the difference.
Someone has a better idea or a link to an example ?
The use of local in Lua is not domoticz related. It's valid for Lua as a programming language. If embedded in an application like domoticz or used as a stand alone language.
Did you actually clicked the link in my post ?
ref
Also this post.
this post
And this example
Code: Select all
local function outer()
function inner() -- This function can be used in this script below this line
return('inner')
end
return 'outer-' .. inner()
end
print('Calling outer() ==>> ' .. outer() ) -->> prints: Calling outer() ==>> outer-inner
print ('Calling inner() ==>> ' .. tostring(inner() )) -->> prints: Calling inner() ==>> inner
local function outerL()
local function innerL() -- This function can only be used within the scope of outerL
return('innerL')
end
return 'outerL-' .. innerL()
end
print('Calling outerL() ==>> ' .. outerL() ) -->> prints: Calling outerL() ==>> outerL-innerL
print ('Calling innerL() ==>> ' .. tostring(innerL() )) -->> error: attempt to call a nil value (global 'innerL')
Re: Difference local function and function
Posted: Sunday 14 June 2020 13:00
by Bumke1963
Thnx, i am a newbie but i am getting it bit for bit. Now the question remains: where do i declare my own global variables in Domoticz-LUA. I know Lua nows time, device, etc. events. So where to put them ?
Re: Difference local function and function
Posted: Sunday 14 June 2020 13:10
by waaren
Bumke1963 wrote: ↑Sunday 14 June 2020 13:00
Thnx, i am a newbie but i am getting it bit for bit. Now the question remains: where do i declare my own global variables in Domoticz-LUA. I know Lua nows time, device, etc. events. So where to put them ?
Basically; you don't.
They will be lost after your script finished. If you need to keep values between script executions in classic Lua, you need to store them in domoticz uservariables or as an attribute of a device.
in dzVents you can do as in classic Lua and/or use persistent data to store values. Persistent data is stored in OS files between script executions.