Published Oct 18, 2023
Contribute to Docs

The yield() function suspends the current coroutine and returns its arguments to the caller. When subsequently resumed, yield() will return any arguments that were passed to resume(), and pick up where it left off.


  • The yield() function is used to temporarily pause the execution of a coroutine and return control to the coroutine that resumed it.
  • When coroutine.yield() is called, any arguments passed to it are returned by the corresponding coroutine.resume() call that started the coroutine.
  • The arguments passed to coroutine.resume() are available as return values of the yield() function call that suspended the coroutine before.


The example below demonstrates how the yield() function allows the coroutine to pause its execution and restart from the same point.

function myCoroutine()
print("Hello, Codecademy!")
print("How are you?")
co = coroutine.create(myCoroutine)

This will result in the following output:

Hello, Codecademy!
How are you?

All contributors

Looking to contribute?

Learn Lua on Codecademy