Fallback Function
1 min read
Pronunciation
[fôl-bak fungk-shun]
Analogy
Think of the fallback function as a shopkeeper’s default clerk—when a customer arrives with an unrecognized request, the clerk handles it in a generic way.
Definition
Key Points Intro
Fallback functions provide a catch‑all mechanism in smart contracts via:
Key Points
Unnamed function: Declared as `fallback()` with external visibility.
Payable or not: Must be marked `payable` to receive Ether.
Gas stipend: Limited gas when called via `.send` or `.transfer`.
No arguments/return: Cannot accept data or return values.
Example
Technical Deep Dive
The fallback function is invoked if calldata is empty and no `receive()` exists, or if calldata does not match any function selector. It shares a 2300‑gas stipend when called via `transfer`/`send`, sufficient only for logging or simple state changes. Defining logic here can catch erroneous calls or implement proxy forwarding.
Security Warning
Complex logic in fallback can enable reentrancy attacks due to inadvertent external calls; keep it minimal and use `ReentrancyGuard` where needed.
Fallback Function - Related Articles
No related articles for this term.