Payable Functions
In Solidity, a payable function is a function that can receive ether as part of a transaction. It is denoted by the payable
modifier in the function signature. When a payable function is called, the amount of ether sent in the transaction is added to the contract's balance. Payable functions are typically used to accept payments, transfer ether to other addresses, or purchase tokens.
For example, consider the following Solidity function:
In this example, buyToken
is a payable function because it has the payable
modifier in its signature. When buyToken
is called, the amount of ether sent in the transaction is added to the contract's balance. The function can then perform some code to buy tokens or perform other operations that require ether.
It is important to note that not all functions need to be payable. Only functions that are intended to receive ether as part of a transaction should be marked as payable. If a non-payable function receives ether, the transaction will be reverted.
Last updated