What is the difference between a coroutine and a regular function?
Short answer
A regular function runs to completion in one invocation using a single stack frame that's destroyed on return.
Advertisement
AModel answer
A regular function runs to completion in one invocation using a single stack frame that's destroyed on return. A coroutine can suspend and resume, so its state (locals, execution point) is stored in a separate 'coroutine frame' (often heap-allocated) that persists across suspensions until it completes or is destroyed. This lets it pause without blocking a thread.
Real-world example
Unlike a normal function, a coroutine pauses at co_await and lets the thread do other work meanwhile.
Likely follow-ups
- Where is coroutine state stored? | Why can a coroutine pause without blocking?
Related topics