乐闻世界logo
搜索文章和话题

What is the Fallback Function in Solidity?

2月7日 00:04

In Solidity, the Fallback Function is a special function that has no name, accepts no parameters, and returns no value. It is invoked when the contract receives Ether but no other function matches, or when the function signature does not match any defined function in the contract. It is typically used to directly receive Ether transfers or as a general exception handler.

In Solidity versions after 0.6.x, to enhance clarity and safety of contract code, the fallback functions are categorized into two types:

  1. Receive function - Specifically for handling ETH transfers without any data. This function must be declared with receive() external payable.
  2. Fallback function - Triggered when no match is found for the receive function, or when a non-existent function is called, or when Ether is sent with data included in the call. This function is declared with fallback() external payable.

These functions provide flexibility and safety, enabling smart contracts to appropriately respond based on whether the call is for pure ETH transfers or ETH transfers with data.

标签:Solidity