Solidity Fallback Function: Everything You Need to Know
This article aims to provide clear understanding regarding solidity fallback function in order to provide valuable concepts to the learner regarding the fallback functions and their working respectively.
What Is Solidity Fallback Function?
Solidity fallback functions lack a name, parameters, or return values.
The fallback function is a special function in Solidity contracts that is called when the contract receives funds or when an invalid function call is made to the contract.
It is a catch-all function that allows contracts to receive ether and also handles unrecognized function calls.
The following scenarios can lead to its execution:
- The function identifier is not one of the available functions in smart contracts.
- If no data was provided with the function call.
- It is possible to assign only one unnamed function to a contract.
Solidity Fallback Function Properties
- It is an unnamed function.
- It is impossible for them to accept arguments.
- It is impossible to return anything.
- In a smart contract, only one fallback function can exist.
- Marking it as external is mandatory.
- You should mark it as payable. The contract will throw an exception if ether is received without any data.
- If invoked by another function, it will be limited to 2300 gas.
The fallback function is called in two cases:
- When a user sends ether to a contract without specifying a function call.
- When a user sends ether to a contract and specifies an invalid function call.
In the first case, the fallback function is called automatically. In the second case, the fallback function is called explicitly by the EVM (Ethereum Virtual Machine).
How to Use Fallback Function?
The fallback function can be used in various ways, depending on the requirements of the contract.
Here are some examples of how the fallback function can be used:
Use Cases | Overview |
Receive Ether | The fallback function can be used to receive ether from external accounts or contracts. |
Reject Ether | The fallback function can be used to reject ether from external accounts or contracts. |
Redirect Ether | The fallback function can be used to redirect ether to another contract. |
Execute Code | The fallback function can be used to execute code when an invalid function call is made to the contract. |
The given programme shows the declaration of a fall back function. The output of this method can only be visible in the log section:
Example: 
Example: 
Example: 
Conclusion
Solidity Fallback functions are essential to Solidity smart contracts. With this feature, the contract can process incoming Ether transactions and function calls that are not explicitly defined in the contract.
Contract users may use fallback functions when dealing with custom payment and refund logic, handling unexpected inputs gracefully, or providing a fallback mechanism in case of errors.
The use of fallback functions should, however, be implemented properly to avoid introducing security vulnerabilities. A contract should avoid sending Ether or making external calls from the fallback function, since this can potentially be exploited by attackers.