return question

Specifically, what does “return” mean/do? For instance, in this block:

–Function to spawn an object
local function spawn()
local object = display.newImage(“myImage.png”)
return object ----------------------------------- < – this “return” here
end

–Create a table to hold our spawns
local spawnTable = {}

–Spawn two objects
for i = 1, 2 do
spawnTable[i] = spawn()
end
What is “return object” doing? Can’t seem to find any documentation specific to this term. Sure appreciate any help. [import]uid: 96383 topic_id: 17407 reply_id: 317407[/import]

At the end of the function the object you just created is what is “returned”.

So the code above is spawning the object using the spawnTable[i] = spawn() line, which says spawnTable[i] is the object being returned.

That’s kind of a newbie way of explaining it (what I tend to do) but if you want something more advanced I can try to find a link for you - just let me know :slight_smile: [import]uid: 52491 topic_id: 17407 reply_id: 65879[/import]

Hey Peach, I guess what I’m wondering is this - the author of the tutorial makes a destination between this code:

local function spawn()
local object = display.newImage(“myImage.png”)
end

And this code:
local function spawn()
local object = display.newImage(“myImage.png”)
return object
end
but I can’t figure out functionally what the “return object” part actually does differently. Is there a real newbie way to explain that? Since real newbie definetly describes me… :slight_smile: [import]uid: 96383 topic_id: 17407 reply_id: 65882[/import]

I’m interested in hearing Peach’s thought on this. That said, I do return object when I want the function to return the object.

So, when I want the function to spawn an object, I’d definitely include return object , and use the function like this:

[lua]local function spawnMyObject()
local object = display.newImage(“myImage.png”)
return object
end

local myObject1 = spawnMyObject()[/lua]

I don’t know what myObject1 would be like if spwanMyObject doesn’t return the object… if spawnMyObject does not return an object, would myObject1 become the function that does the same thing as the spawnMyObject function?

Naomi
[import]uid: 67217 topic_id: 17407 reply_id: 65888[/import]

By returning the object you can use it in the calling function, insert it into a display group, etc…

So you could do something like this:
[lua]local function spawn()
local object = display.newImage(“myImage.png”)
return object
end

local function addEnemy()
thisEnemy = spawn() --thisEnemy now has the value of the returned object.
thisEnemy.alpha = .2 --You can now change the opacity on it
thisEnemy.id = ‘enemy12’ --Add an ID to it so you can track it later.
localGroup:insert(thisEnemy) --insert it into a display group.
end[/lua]

If the object wasn’t returned, you couldn’t do much with the object created in spawn().

Dunno if that helps or not? [import]uid: 19193 topic_id: 17407 reply_id: 65889[/import]

@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]

—Duplicate---- cannot delete, where did the delete button go??? [import]uid: 3826 topic_id: 17407 reply_id: 65896[/import]

It looks like I’m a little late - between Scott and Jayant this seems well covered :slight_smile: [import]uid: 52491 topic_id: 17407 reply_id: 65908[/import]

Cool. Thank you, guys! Good to learn new things (and to know exactly why I code the way I do, rather than coding the way I do because my gut tells me to do so.)

Cheers,
Naomi [import]uid: 67217 topic_id: 17407 reply_id: 66001[/import]

Thanks for the insights. But I guess my question is even more general and newbie ish than anyone can even believe :slight_smile:

I have a fair understanding of what functionality “return object” is providing here - I’ve tried the code with and without the “return,” so that much is clear. But more generally, in a syntactic sense, what does “return” mean? Because it is used in other places in other ways, such as “return true.”

When I use “return,” what am I saying? Hope this makes sense. [import]uid: 96383 topic_id: 17407 reply_id: 66052[/import]

Return, give back, respond with, answer with, hand back, pass back.

If you go to an ATM it returns the amount of money specified.

[lua]return true[/lua] means that true (the condition) is being passed back, or returned.

Does that help at all?

I’m trying to think of ways to make it click but this is something I’ve never tried to explain before, heh.

Peach :slight_smile: [import]uid: 52491 topic_id: 17407 reply_id: 66178[/import]