@khincker,
This comes in from the olden days of programming where you had
Functions and Procedures
Procedures were code that were executed and did something
Functions on the other hand executed some code and returned a value
just like in math f(x) (if you do understand math [Sorry, many developers here are new and generally in a lower age group that have limited understanding of logic, programming and math] so rather than assume that you are aware of this, it is best to explain)
so think of the two situations
[lua]local function add(a,b)
end[/lua]
and
[lua]local function display(name)
end[/lua]
you can from the name of the functions determine that the function add should return the result of the addition of a and b
where as the function display should just display the name somewhere and not necessarily return anything
Now to the specific example that you had
[lua]local function spawn()
local obj = display.newImage(“newimage.png”)
end[/lua]
[lua]local function spawn()
local obj = display.newImage(“newimage.png”)
return obj
end[/lua]
If you understand scopes, then you will also know that the local obj shall not exist outside of the function in either of the cases, so unless we store that somewhere, we would have lost that, so we return it to the calling code that could use it.
the correct way to handle the first case would be
[lua]local array={}
local index = 1
local function spawn()
local obj = display.newImage(“newImage”)
array[index] = obj
index = index + 1
end[/lua]
in the second method, the correct way to handle it would be
[lua]local array={}
local i
local function spawn()
local obj = display.newImage(“newImage”)
return obj
end
for i=1,10 do
array[i] = spawn()
end[/lua] [import]uid: 3826 topic_id: 17407 reply_id: 65897[/import]