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:
- Has no name or arguments.
- If it is not marked payable, the contract will throw an exception if it receives plain ether without data.
- Can not return anything.
- Can be defined once per contract.
- It is also executed if the caller meant to call a function that is not available
- It is mandatory to mark it external.
- 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.
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:
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
- A contract receives only Ether and no data.
- 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; }