This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MrBarry's profile picture

Question about variable declaration

Started by MrBarry, 30 September 2012 - 02:05 AM
MrBarry #1
Posted 30 September 2012 - 04:05 AM
Which way is better? Is there a difference?


local foo
while true do
foo = fooFunction()
end


while true do
local foo = fooFunction()
end
Pharap #2
Posted 30 September 2012 - 04:17 AM
Ok, I have no clue what you are getting at.
You're trying to give a function a different name to run by?
I'm getting the feeling you read ahead a bit and have confused yourself (either way you've confused me).

In short, can you make the question a bit clearer, with more detail?
Kingdaro #3
Posted 30 September 2012 - 05:06 AM
It actually depends on the situation. If you need to access foo outside of the while loop, the first option is your best bet. However if the variable is unique to only the while loop, and you don't need it anywhere else, it's better to localize it inside of the loop.
MrBarry #4
Posted 30 September 2012 - 11:56 AM
Sorry, yeah that was a bit unclear. My question was is there any difference in performance.

I think this stackoverflow post might have answered my question.

And thanks Kingdaro. I didn't know you could localize a variable to inside a loop. Testing shows me that in the second example foo is nil after the loop ends.
Cloudy #5
Posted 30 September 2012 - 12:15 PM
As far as I understand it there is a small performance penalty from localising it over and over. Hence the first way would be better.

However we are probably talking milliseconds if even that!