0

I want to know what's wrong with my function, 'cause Visual Studio Code give me warning :

function state mutability can be restricted to view

this is my function :

  function verifyVote(address pers) public returns (uint){

    for (uint i = 0; i < personne.length; i++){
        if (pers == personne[i].own) {
            if (personne[i].isVoted == true){
                return 1;
            }
            else{
                return 0;
            }
        }
    }

    return 404;
} 
1
  • Since your function doesn't modify any state in the contract, it can be changes to a view function. VS code is suggesting a best practice and your code will compile even without this. For more information about function state mutability, refer solidity documentation at solidity.readthedocs.io/en/latest/contracts.html#view-functions
    – Manoj P R
    Commented Nov 7, 2019 at 11:01

2 Answers 2

3

Your function doesn't modify the state, so it can be declared as a view function.

From the documentation:

Functions can be declared view in which case they promise not to modify the state.

function verifyVote(address pers) public view returns (uint){
    for (uint i = 0; i < personne.length; i++){
        if (pers == personne[i].own) {
            if (personne[i].isVoted == true){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    return 404;
}
0
0

Your contract contains a state variable personne[].

Your function verifyVote() only reads values from your contract storage, by looping through each elements of the personne[] array. It does not modify the values.

From the Solidity documentation

Functions can be declared view in which case they promise not to modify the state.

  • view = read, write
  • pure = read, write
  • No specifier = read, write

So the view specifier ensures that your function is restricted to read-only.

3
  • 2
    There is, because calling this function from an off-chain script requires signing and sending a transaction, and then paying gas fee for it to be executed. In addition to that, the value returned to the off-chain is going to be the receipt of that transaction, rather than the uint intended by the author. And in order to make that value available for the off-chain script, the author will need to emit it in an event (which will furthermore increase the gas cost). So in short, there is a lot wrong in not specifying pure or view where possible. Commented Nov 7, 2019 at 11:14
  • @goodvibration I am jumping on your last sentence : So in short, there is a lot wrong in not specifying pure or view where possible.. Emiting an event is an operation that modifies the state. Therefore, the function cannot be declared as view if it emits an event. So does that mean that the function should have "no specifier" if the aim is to make the value available to an off-chain script (via the event log) ?
    – CJ42
    Commented Nov 7, 2019 at 11:35
  • 1
    On the contrary, the function should be declared view, so that the off-chain script will be able to obtain the returned value. Emitting the returned value in an event should be reserved only to cases where the function cannot be declared view (AND - the returned value is expected to be required by an off-chain script). Commented Nov 7, 2019 at 11:49

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.