|
| 22 Feb 2017 08:40 PM |
why use local? when i dont use it it still works ?
|
|
|
| Report Abuse |
|
|
|
| 22 Feb 2017 08:41 PM |
scope limitation, memory management, quicker to access they're better than globals in every way
|
|
|
| Report Abuse |
|
|
morash
|
  |
| Joined: 22 May 2010 |
| Total Posts: 5834 |
|
|
| 22 Feb 2017 08:42 PM |
| To protect scope. Of course, if you want to trample all over your global namespace, that is fine too. |
|
|
| Report Abuse |
|
|
|
| 22 Feb 2017 08:44 PM |
Local variables allow multiple functions to reference a variable with the same name without affecting the same instance.
|
|
|
| Report Abuse |
|
|
|
| 22 Feb 2017 08:45 PM |
"Local variables allow multiple functions to reference a variable with the same name without affecting the same instance."
but if that's desired then you can use forward declaration to mimic globals
|
|
|
| Report Abuse |
|
|
EvanHolt
|
  |
| Joined: 06 Sep 2008 |
| Total Posts: 1264 |
|
|
| 22 Feb 2017 09:42 PM |
Because.
[ George Orwell taught me math! 2 + 2 = 5 ] |
|
|
| Report Abuse |
|
|
|
| 22 Feb 2017 09:54 PM |
Simply put, local variables are encapsulated by scope and so they may be reused when the scope ends, so in most cases it is favorable to use local variables over global variables.
Additionally, they are also faster to read and write to as they are stored on the Lua stack (along with function parametetrs), where as global variables are stored in the enclosing function's environment, which is a regular Lua table.
Lua has an default maximum number of 200 local variables per scope.
You should also read the page at this link: lua .org/ pil/4.2 .html |
|
|
| Report Abuse |
|
|