Solidity Functions: Special Functions
Learn about special functions in Solidity.
We'll cover the following...
Special function types
Solidity allows for the optional definition of special functions, including the following:
Payable functions
View and pure functions
Constructors
Payable functions
Declaring a function of the payable type, specified with the payable
keyword, makes it able to receive ETH as part of the transaction calling it. This is similar to the address payable
type for variables.
For instance, we have the following payment contract, which can be used to receive fees for a service.
Press + to interact
pragma solidity >=0.5.0 <0.9.0;contract examplePayableFunction {uint256 public constant fee = 0.1 ether;mapping(address => bool) public paid;function pay() public payable {require(msg.value == fee && paid[msg.sender] != true);paid[msg.sender] = true;}}
Line 3: We define the fee to be paid as a constant state variable, say with a value of 0.1 ETH. Here, we use the reserved
ether
keyword for ETH units.Line 4: ...