The fallback Functions

Learn how fallback functions work in Solidity.

In the world of smart contracts, unexpected situations can arise. A user might attempt to call a function that doesn’t exist, or Ether might be sent directly to the contract without specifying a function. To handle these scenarios gracefully, Solidity provides a special function called the fallback function. This function is the safety net of smart contracts. It provides a way to handle unexpected messages and to ensure that the contract remains in a valid state. It’s executed when a contract receives a message not handled by its other functions.

A fallback function can be virtual, overridden, and modified. Whenever a call to the contract is made, the fallback function is run if none of the other functions match the specified function signature or if no data was sent and there’s no receive Ether function. The fallback function always gets data, but it must also be marked payable to receive Ether.

Characteristics of the fallback functions

  • No parameters or return values: The fallback functions accept no ...