[Ethereum] payable() function In solidity


I have read many examples of payable functions, but I didn't understand any of them. Also, they talked about modifier in payable. My questions are:

  1. What exactly does payable() function do?
  2. Does this function take arguments?

Best Answer

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:


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;