[Ethereum] Solidity: Function state mutability warning

solidity

I am testing some compiler options with solcjs and solc and I am just coding basic outlines of contracts. I am getting warning Function state mutability can be restricted to view, has anyone any comments?

root@ubuntu-s-2vcpu-4gb-lon1-01:/geth/mybc/geth# more AddressContract.sol
pragma solidity ^0.4.19;

contract AddressContract {
    string private myAddress;
    function getAddress() public returns (string) {
        return myAddress;
   }
}

root@ubuntu-s-2vcpu-4gb-lon1-01:/geth/mybc/geth# vi AddressContract.sol
root@ubuntu-s-2vcpu-4gb-lon1-01:/geth/mybc/geth# solcjs --bin 
AddressContract.sol
AddressContract.sol:5:1: Warning: Function state mutability can be 
restricted to view
function getAddress() public returns (string) {
^
Spanning multiple lines.

 root@ubuntu-s-2vcpu-4gb-lon1-01:/geth/mybc/geth#

Best Answer

This is a warning not an error. You can ignore it and nothing bad will happen.

However, it is helpfully telling you that since your function doesn't change the state, you can mark it as view. See this answer for what that means and why it's a good idea: Is there any profit from using pure and view functions modifiers?

Related Topic