7

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).

3 Answers 3

3

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.

1
  • 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. Mar 11, 2018 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;
}
}
0

This worked for me without any errors

pragma solidity ^0.8.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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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