Difference between Coroutine and Function

Are there any difference to a Coroutine and a function?

Here is my Code:

-- Coroutine local function doCoroutining() -- this has been called in scene:create local function mover() while ismOving do print("IS MOVING") coroutine.yield() end end moveMe = coroutine.create(mover) end -- Function local function doFfunction() print("IN FUNCTION") end -- onKeyEvent Runtime is created in "did show scene" local function onKeyEvent( event ) -- Print which key was pressed down/up local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase print( message ) coroutine.resume(moveMe) doFfunction() return false end

there are no errors here, it’s just, they do the same, what’s the difference? Why should I use a Coroutine vs a Function?

I believe the main point would be if you were doing a large task that would tie up the CPU for too long if you did it in one go - say processing 100,000 items in a loop. You could loop through a chunk of the items, and then call yield, to give your program enough time to do other stuff and update the screen.

Of course you can do this using an enterFrame listener too, and just keep track of where you are in the loop and process the next ‘n’ items in each frame.

Are there any difference to a Coroutine and a function?

Yes!  (and I should just leave it at that :D)  They’re not the same thing at all, and there are many differences.

A coroutine is a thread, while a function is a, um, well:  function.  A coroutine requires a function during its create, but that function is not itself the coroutine, though it appears to “become” the coroutine once “wrapped” by coroutine.create().

A coroutine can be yield()'d “within” the function, and resumed “within” the function.  In your sample case, where both methods simply print() and return, there is little practical difference – but that’s just because your usage is so trivial.

Here’s something that CAN’T be done with a plain-ordinary function:

local co = coroutine.create(function() for i = 1, 10 do print(i) coroutine.yield() end end) Runtime:addEventListener("enterFrame", function(e) -- this will print a sequential number on each of the first 10 frames -- note that the coroutine's function "remembers" it's state after yield -- and can resume "within" the for loop coroutine.resume(co) end)

Sure, there are equivalent ways you could use a plain-ordinary function to “count to ten” via an enterFrame listener (fe: use a closure variable as index, manually increment and test), but any such work-around will be fundamentally different from how coroutines work.

I was just reading the book “Programming in Lua, fourth edition” and it has some very well written chapters about the functionality of coroutines within lua. The first edition of the book with the chapter on coroutines is free to view here - https://www.lua.org/pil/9.1.html

I think it really helped me understand their usage and how to implement them.  :smiley:

I believe the main point would be if you were doing a large task that would tie up the CPU for too long if you did it in one go - say processing 100,000 items in a loop. You could loop through a chunk of the items, and then call yield, to give your program enough time to do other stuff and update the screen.

Of course you can do this using an enterFrame listener too, and just keep track of where you are in the loop and process the next ‘n’ items in each frame.

Are there any difference to a Coroutine and a function?

Yes!  (and I should just leave it at that :D)  They’re not the same thing at all, and there are many differences.

A coroutine is a thread, while a function is a, um, well:  function.  A coroutine requires a function during its create, but that function is not itself the coroutine, though it appears to “become” the coroutine once “wrapped” by coroutine.create().

A coroutine can be yield()'d “within” the function, and resumed “within” the function.  In your sample case, where both methods simply print() and return, there is little practical difference – but that’s just because your usage is so trivial.

Here’s something that CAN’T be done with a plain-ordinary function:

local co = coroutine.create(function() for i = 1, 10 do print(i) coroutine.yield() end end) Runtime:addEventListener("enterFrame", function(e) -- this will print a sequential number on each of the first 10 frames -- note that the coroutine's function "remembers" it's state after yield -- and can resume "within" the for loop coroutine.resume(co) end)

Sure, there are equivalent ways you could use a plain-ordinary function to “count to ten” via an enterFrame listener (fe: use a closure variable as index, manually increment and test), but any such work-around will be fundamentally different from how coroutines work.

I was just reading the book “Programming in Lua, fourth edition” and it has some very well written chapters about the functionality of coroutines within lua. The first edition of the book with the chapter on coroutines is free to view here - https://www.lua.org/pil/9.1.html

I think it really helped me understand their usage and how to implement them.  :smiley: