Contract state modifying statements
- Setting the value in storage variable
- Emitting of events from the function
- Creating a new contract instance
- Sending Ethers
- Using low level calls OR EVM assembly
- Using selfdestruct
- Calling functions that modify the state with any of the above
View statements e.g. function getSomethingFixed() public view returns (uint)
- Allowed to read the
- NOT allowed to use any state change statement
Pure statements e.g. function returnConstantFixed() public pure returns (uint)
- NOT allowed to read the storage
- NOT allowed to use any state change statement
Bad function getSomething() public returns(uint) { someValue = 10 // Shouldn't change the state of storage in GET function return someValue; }
Good function getSomethingFixed() public view returns(uint) { someValue = 10 // With view modifier, a state change in the function will cause compilation error, forcing developer to fix the error return someValue; }
Bad function returnConstant() public returns(uint) { return someValue + 100 // This is not a constant }
Good function returnConstantFixed() public pure returns(uint) { return someValue + 100 // With pure modifier, a state change in the function will cause compilation error, forcing developer to fix the error
return 100 // This will return constant }