Solidity Fallback Function

The solidity fallback function is executed when no other functions match the function identifier or no data is provided. It is only assigned to unnamed functions and is executed when a contract receives plain Ether without data. The fallback function must be marked payable to add Ether to the total balance of the contract. Without such a function, the contract cannot receive Ether through regular transactions and throws an exception

Key Points
  • Declare with fallback() and have no arguments.
  • If it is not marked payable, the contract will throw an exception if it receives plain ether without data.
  • Can not return anything.
  • Can be defined once per contract.
  • It is also executed if the caller meant to call a function that is not available or receive() does not exist or msg.data is not empty.
  • It is mandatory to mark it external.
  • It is limited to 2300 gas when called by another function by using transfer() or send() method . It is so for as to make this function call as cheap as possible.
fallback()

This function is called for all messages sent to this contract, except plain Ether transfers (there is no other function except the receive function). Any call with non-empty calldata to this contract will execute.

receive()

This function is called for plain Ether transfers, i.e. for every call with empty calldata.

Question / Answer

The solidity fallback function is executed if none of the other functions match the function identifier or no data was provided with the function call.