Skip to content
Related Articles

Related Articles

Solidity – Fall Back Function
  • Last Updated : 18 Sep, 2020

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. Has no name or 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
  6. It is mandatory to mark it external.
  7. It is limited to 2300 gas when called by another function. 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




pragma solidity ^0.4.0;
  
// Creating a contract
contract GeeksForGeeks 
{
    // Declaring the state variable
    uint x; 
      
    // Mapping of addresses to their balances
    mapping(address => uint) balance; 
  
    // Creating a constructor 
    constructor() public
    {
        // Set x to default 
        // value of 10
        x=10; 
  
    }
  
    // Creating a function 
    function SetX(uint _x) public returns(bool)
    {
        // Set x to the 
        // value sent
        x=_x; 
        return true;
    }
      
    // This fallback function 
    // will keep all the Ether
    function() public payable
    {
        balance[msg.sender] += msg.value;
    }
  
// Creating the sender contract
contract Sender 
{
  function transfer() public payable 
  {
      // Address of GeeksForGeeks contract
      address _receiver = 
              0xbcc0185441de06F0452D45AEd6Ad8b98017796fb;  
                
      // Transfers 100 Eth to above contract          
      _receiver.transfer(100); 
  }
}

Output:



Fallback function

Output

Explanation:

1. Contract GeeksForGeeks: It has a variable x which is set to the default value 10 in the constructor(). The contract has a function called SetX(uint _x) which sets the function value to the desired parameter sent during the function call. The below declaration creates address to value map called balance which maps the addresses to their balance. 

mapping(address => uint) balance;

2. Contract Sender: This is a completely independent and unrelated contract. It sends a value in Ether to the contract GeeksForGeeks. The contract does not know the mechanism of the GeeksForGeeks contract. Sending a transaction without any message and only Ether can cause an error.
The below statements declare a variable _receiver of the address type. It explicitly stores the address of contract GeeksForGeeks. It then uses address.transfer(value) to transfer Ether to the contract.

address _receiver = 0xbcc0185441de06F0452D45AEd6Ad8b98017796fb;  //Address of GeeksForGeeks contract
_receiver.transfer(100); 

3. Function() public payable: 

The function below is a fallback function. It is declared payable which allows it to accept transfer value. It is called in two cases

  1. A contract receives only Ether and no data.
  2. No function calls matched even though the account received data.

This helps us to protect the function from throwing an error. In this program, the contract GeeksForGeeks receives only Ether and the fallback function uses the value received to add to the balance related to the sending address.

function() public payable
    {
        balance[msg.sender] += msg.value;
    }
My Personal Notes arrow_drop_up
Recommended Articles
Page :