First, payable
is a modifier that can be added to a function. What you are most likely misinterpreting is a use case like:
function () public payable {}
It's impossible to have payable()
as a function name as it is a reserved keyword. You may use payable
only in addition to existing functions like:
function deposit() payable {}
function register(address sender) payable {}
Second, payable
allows a function to receive ether while being called as stated in docs. It's mandatory to include the payable
keyword from Solidity 0.4.x. If you try to send ether using call, as follows:
token.foo.call.value("ETH_TO_BE_SENT")("ADDITIONAL_DATA")
to a function without a payable
keyword, the transaction will be rejected.
Usually, there is a no name function to accept ether to be sent to a contract which is called a fallback function:
function () payable {}
But you may have more than one payable
annotated functions that are used to perform different tasks, like registering a deposit to your contract:
function deposit() payable {
deposits[msg.sender] += msg.value;
};
Anyone can call any public function on your contract, by sending a transaction to that contract and including "data" that names that function.
If someone sends a transaction with ether to a function, and the contract did not mark the function as payable, then the call will be rejected. If the contract did mark the function as payable, though, that ether will be deposited into the contract's control. (and whatever code is in the function will run)
The fallback function is called when you send a transaction to a contract, and don't identify any function (in other words, "data" is empty). All the payable rules in the previous paragraph apply. Nothing prevents you from sending 0 ether to the fallback function.
Best Answer
The function without a name:
function ()
is called the fallback function. It is executed when the contract receives some ETH without a function being explicitly called.Putting
revert();
in it means that you cannot send ETH to the contract without explicitly calling a payable function.