Function values

Hi, i’ve been searching but i couldn’t really find anything.
How do you change values inside a function? something like:

[lua]local function example()
apple = 1
end
example.apple = 2[/lua]

Is it even possible to change a variable from a function?
Thanks [import]uid: 108461 topic_id: 21272 reply_id: 321272[/import]

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]

Good Job Rob,:slight_smile:

undecode here some more for ya…:wink:

--\*\* copy and go code ;)  
--\*\* some simple code to add or subtract apples..using a function ;)  
\_G.totalApples = 16; --Global depending program needs and scope  
--\*\* local totalApples = 16; --\*\*Or could be local like this  
local function addMoreApples(howManyFound)  
  
 --\*\* you could add / subtract / ...etc  
 totalApples = (totalApples + howManyFound);  
  
 --\*\* then return to calling function  
 return totalApples;  
  
end  
  
local function main()  
  
 local applesReturned = 0;  
  
 print("\nTotal apples when we started: ", totalApples);  
  
 --\*\* we found some more apples under the tree..;)  
 applesReturned = addMoreApples(14);  
  
  
 print("\nThis Way also! ", applesReturned);  
  
  
 print("\nTotal apples this way too!", addMoreApples(14));  
  
  
 print("\nTotal apples when we ended: ", totalApples);  
  
  
 --\*\* main()'s return to system  
 return 0;  
  
 --\*\* anyway there's alot of different ways to do what  
 --\*\* you need / want  
 --\*\* just stay with-in the .lua/corona programming lanq. ;)  
  
end  
  
--\*\* run our main()   
main();  
  

Happy Coding :wink:
Larry

[import]uid: 107633 topic_id: 21272 reply_id: 84262[/import]