Example 1.
local apple = 1
local function(apple)
apple = 2
end
print(apple) -- prints 2
Example 2.
local apple = 1
local function changeApple()
apple = 2
end
print(apple) -- prints 2
Example 3
local apple = 1
local function changeApple()
return 2
end
apple = changeApple()
print(apple) -- prints 2
In your example
function example()
apple = 1
end
example.apple = 2
We have several issues going on.
First we don’t know what "apple is when you reference it in side of function named “excample”. The assumption here is that “apple” is a global variable. But “apple” could be a simple number or string or it could be a table or it could be a function. Assuming its a simple variable, things will behave like you expect them. But if apple is a table (which includes loading a graphic called apple, i.e.:
apple = display.newImage(“apple.png”)
or a function, those chunks of content would be replace by an interger named 1.
Your second issue is using:
example.apple = 2
In this case, someone looking at the code (including the compiler) will see example as being a table with a key-value pair of apple and 2. But previously you defined “example” as a function. These types are not compatible and this would likely through an error since example is not a table and you can’t reference key-value pairs from a function or simple variable.
[import]uid: 19626 topic_id: 21272 reply_id: 84254[/import]