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