1

I have a function call which only reads data from contract but it still costs gas to execute:

   function foo(int256 x) public returns (bool) {
    if(tuna[x].validOnce == true){
      if(dog[x] == true)
        return true;
      else
         return false;
    }

    else
      return true;

 }

I ve read about pure and view that I can add to my method header. However Im not sure if this is a case in point where I need to use such headers?

0

Yes.

function foo(int256 x) public view returns (bool) {
  if(tuna[x].validOnce == true){
    if(dog[x] == true)
      return true;
    else
      return false;
  } else return true;
}

This will return the value instead of a transaction hash. Gas accounnting will still unfold but gas will be returned unless this is called from another contract that is not doing a read-only operation.

Incidentally, you use view instead of pure because this function reads data.

Have a look here, as well: When to use "View" and "Pure" in place of "Constant"

Hope it helps.

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.