4

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#
5

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?

  • 2
    That worked. I coded function getAddress() public view returns (string) – Trevor Lee Oakley Feb 14 '18 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.