The pure, view, and payable functions
Functions can be declared as pure
, view
, or payable
, depending on whether it can receive Ether or whether it needs to access the state. Some earlier versions of Solidity had a constant as the alias to view, but it was dropped in version 0.5.0:
- Pure function: A
pure
function means it won’t read or modify the state - View function: A
view
function means it won’t alter the storage state in any way, but you can view it - Payable function: A
payable
function means it can receive Ether
Here is an example of the pure
, view
, and payable
functions:
//pure   function add(uint a, uint b) public pure returns(uint) {   } //view   function add(uint a, uint b) public view returns(uint) {   } // payable function send() payable { }
The state means anything EVM holds beyond the input parameters, including state variables, events, contracts, Ether, and more. The following are...