Questions tagged
Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning.

A coroutine is a pair of code sequences that can interchange control.

In hardware this is accomplished by interchanging the stack top and the program counter. Co-routines were extensively used inside operating systems.

Coroutines are well-suited for creating iterators and pipes. Subroutines can return only once; in contrast, coroutines can return (yield) several times. Yielding returns the result to the calling coroutine and gives it back control, like a usual subroutine. However, the next time the coroutine is called, the execution starts just after the yield call.

Related tags