A callback is a function that you pass into another piece of code so it can be called later, often after some work finishes or when an event happens.

H1: What Is a Callback?

In programming, a callback is a function given as an argument to another function or API, which will “call back” your function at some later point. This lets you customize what happens after an operation completes without changing the original library or function.

Think of it like leaving instructions: “When you’re done loading the data, run this function.” The code that receives your callback decides when (and sometimes how often) to run it.

H2: Simple Real-World Analogy

  • You drop a suit off at a tailor and say, “Call me when it’s ready.”
  • Your phone number is the callback: information the tailor uses later to contact you.
  • The tailor finishes the work, then uses that number to call you back.

In code, your function is that phone number: you give it to another function, and when its job is done, it “calls” your function.

H2: Key Properties of Callbacks

  • The callback is a function passed as an argument to another function.
  • The receiving function stores that function reference and invokes it at some later time.
  • The callback may get parameters (like data or error info) and may return a value that affects further behavior.
  • Callbacks are heavily used in asynchronous code (network requests, timers, file I/O, UI events).

H2: A Few Language Examples

HTML Table: Callback Examples

[2][10][3] [10][2][5] [9] [7]
Language / Context How callback works
JavaScript – events You pass a function to addEventListener; the browser calls it when the event (like click) fires.
JavaScript – async You pass a function to things like setTimeout; it runs after the delay or after data is fetched.
C – function pointers You pass a function’s address (via function pointer) to another function, which calls it through that pointer.
Python – higher‑order funcs You pass a function into another function (for events, async tasks, or library hooks), and it gets run later.

H2: Why Callbacks Matter Today

  • Asynchronous programming: Avoid blocking while waiting for network, disk, or timer operations.
  • Event-driven apps: UI frameworks and web browsers rely on callbacks for clicks, keypresses, and other events.
  • Customization hooks: Libraries expose callback “hooks” so you can plug in your own behavior without modifying their code.

Modern patterns like promises and async/await in JavaScript were partly created to tame deeply nested callbacks (“callback hell”), but the core idea—passing a function to be run later—is still the same.

H2: Mini Story Example

Imagine a news app that fetches the latest headlines.
It sends a network request and passes a callback function that says, “When the data arrives, render the headlines on screen.” While the app waits, it can handle user scrolling or button presses, and when the response comes back, the callback runs and updates the interface with the newest stories.

TL;DR: A callback is a function you hand to other code so it can run your function later—often after some task completes or when an event happens—making programs flexible and event-driven.

Information gathered from public forums or data available on the internet and portrayed here.