Continuation-passing style (CPS) is a way to write programs so that each part knows what to do next, like following a recipe step by step.
Imagine you're helping your friend bake cookies. Normally, you might say: "First, mix the flour and sugar. Then add the eggs." But in CPS, it's more like this: "Mix the flour and sugar, and then tell me what to do next." So instead of knowing all the steps at once, each part just tells the next step when it’s done.
Like a Message Chain
Think of CPS as passing a message from one person to another. You start with the first task, say, mixing flour and sugar, and when that's done, you pass a note (the continuation) to the next person, who knows what comes next: adding eggs, then baking.
Each time something happens, it sends a message to the next step, like passing a baton in a relay race. This makes programs easier to manage because each part only focuses on one thing at a time, and that’s what makes CPS so helpful!
Examples
- A baker passes the next step of making a cake to another worker instead of doing it all by themselves.
- A person tells their friend what to do next after they finish their task.
- Passing notes in class to let someone know what to do next.
Ask a question
See also
- How Does 6 Coding Concepts for Absolute Beginners Work?
- How do computers read code?
- How Does 6 Coding Concepts You MUST Know For Beginners Work?
- How does a Computer understand your Program?
- How Does 99% of Developers Don't Get Semaphores Work?