You create a coroutine with a call to . Its sole argument is a function that is the main function of the coroutine. The create function only creates a new coroutine and returns a handle to it (an object of type thread); it does not start the coroutine execution.

    A coroutine can terminate its execution in two ways: normally, when its main function returns (explicitly or implicitly, after the last instruction); and abnormally, if there is an unprotected error. In the first case, coroutine.resume returns true, plus any values returned by the coroutine main function. In case of errors, returns false plus an error message.

    Like coroutine.create, the function also creates a coroutine, but instead of returning the coroutine itself, it returns a function that, when called, resumes the coroutine. Any arguments passed to this function go as extra arguments to coroutine.resume. returns all the values returned by coroutine.resume, except the first one (the boolean error code). Unlike , coroutine.wrap does not catch errors; any error is propagated to the caller.

    When you run it, it produces the following output:

    1. co-body 1 10
    2. foo 2
    3. main true 4
    4. co-body r
    5. co-body x y
    6. main true 10 end