What is a Fallback Function?
In the Solidity programming language, a fallback function is a special type of function that is automatically called whenever a contract receives a message that is not handled by any of the contract's other functions. The fallback function is defined using the fallback keyword, and it does not take any arguments. This function is useful for handling transactions that are sent to the contract without specifying a particular function to call.
For example, a fallback function might be used to update the contract's internal state or to send a message to the sender of the transaction. Here is an example of a simple fallback function in Solidity:
In this example, the fallback function simply sends a message to the sender of the transaction to let them know that the transaction was received. However, the fallback function can be used to perform any number of actions in response to an incoming transaction. It is important to note that the fallback function must be marked as "payable" in order to receive Ether, and that it cannot have any arguments.
The Features
These are the features that exist in the functions:
- They have no need for a name.
- They can be externally called.
- Per contract, there can only be one or zero fallback functions.
- The functions are automatically triggered when called by another contract given that the called function has no name.
- The functions also allow for arbitrary logic to be included.
- It can also be triggered if a token(s) is sent to a contract that has fallback functions with no declared receive() function and no accompanying calldata.
The fallback function has changed the Ethereum history in 2016. Read more about it here.