8

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#

1 Answer 1

12

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
  • 2
    That worked. I coded function getAddress() public view returns (string) Commented Feb 14, 2018 at 4:22
  • Thanks for the link, Edmund !! That's what I was looking for.
    – Jose4Linux
    Commented Mar 17, 2022 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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