can someone explain this compilation error

Viewed 279

I am a new blockchain developer as well as Solidity. I am having a function call problem in the code below. The compiler is throwing some warning. Though the code runs fine, I want to understand the error. Thanks.

function add() public view returns(uint256){
    uint256 a= 5;
    uint256 b= 4;
    uint256 sum = a + b;
    return sum;
}
Warning: Function state mutability can be restricted to pure
1 Answers

Your add() function declares the view state mutability. Which means that the function cannot write to the storage (and perform other state changes such as emit events) but it's allowed to read from the storage.

The warning says that you can use more strict state mutability called pure, which disallows the function not only to write to the storage but also to read from it.

Related