2

I am getting this warning in remix. What does this mean? Am I missing something here? Here is the simple code to demonstrate it:

pragma solidity ^0.4.20;

contract HelloWorld
{
string hello = "Hello World";
function sayHello() returns (string)
{
    return hello;
}
}

Now I am getting a warning:

browser/HelloWorld.sol:6:5: Warning: Function state mutability can be restricted to view
function sayHello() returns (string)
^ (Relevant source part starts here and spans across multiple lines).
2

That simply means, that if you set the function state to view, it "knows" that the function only reads from the blockchain and won't change any variables. Nothing has to be changed and as result the gas fee will be a bit lower.

  • The effective gas-cost will go to zero. It also means that callers will get the response hello quickly, instead of a transaction receipt and a need to wait for that transaction to be mined so they can access a getter function or inspect an event (additional contract code not in the example) to even find out what effect the transaction had on state. All good reasons to use view and pure whenever possible. – Rob Hitchens - B9lab Mar 11 '18 at 17:35
1

use view in your function, if you want your function to read passed variable.using view ,the function will not alter the storage state but at the same time will read value.

pragma solidity ^0.5.0;

contract HelloWorld
{
string hello = "Hello World";
function sayHello() public view returns (string memory)
{
    return hello;
}
}

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.