Solidity – Fall Back Function

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. Only one unnamed function can be assigned to a contract and it is executed whenever the contract receives plain Ether without any data. To receive Ether and add it to the total balance of the contract, the fallback function must be marked payable. If no such function exists, the contract cannot receive Ether through regular transactions and will throw an exception.

Properties of a fallback function:

  1. Declare with fallback() and have no arguments.
  2. If it is not marked payable, the contract will throw an exception if it receives plain ether without data.
  3. Can not return anything.
  4. Can be defined once per contract.
  5. 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.
  6. It is mandatory to mark it external.
  7. 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.

Example: In the below example, the Contract is created to demonstrate different conditions for different fallback function.
 

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0; 
/// @title A contract for demonstrate fallback function
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to fallback function receive all the ether
contract w3wiki
{
    string public calledFallbackFun;
 
    // This fallback function
    // will keep all the Ether
    fallback() external payable{
        calledFallbackFun="Fallback function is executed!";
    }
 
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}
 
// Creating the sender contract
contract Sender
{
    function transferEther() public payable
    {
        //paste the deployed contract address of w3wiki smart contract here
        (bool sent, ) = payable(0xD4Fc541236927E2EAf8F27606bD7309C1Fc2cbee).call{value: 2 ether}("Transaction Completed!");
        require(sent, "Transaction Failed!");
    }
 
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}


Output:

Output

Explanation:

  1. First deploy the w3wiki smart contract and paste the deployed smart contract address into the transferEther function of Sender smart contract.
  2. In the Sender smart contract we have also assigned the transfer value of Ether.
  3. After deploying the Sender smart contract, deposit more than or equal to the assigned Ether value in it at the time of calling the transferEther function. 
  4. transferEther function has the call method to transfer the assigned Ether value from Sender smart contract to w3wiki smart contract and fallback function receives these Ethers.
  5. Call the calledFallbackFun function of w3wiki smart contract. It will return a string value “Fallback function is executed!” and also you can check the smart contract balance by using the getBalance function.