Solidity Web3JS – Use of Pure and View in Solidity Explained

blockchainsolidityweb3js

I have a function:

function test(uint value) {
    if (balances[msg.sender] < value)
        return;

    balances[msg.sender] -= value;
}

I understand that reading from the blockchain only consume 0 gas, for View and Pure function.

So my question is: For the function above will I have to pay for gas if I have insufficient balance and return early (no transaction made).

Best Answer

Since the function code you provided is not defined as view or pure, this becomes "write" function. Meaning that you have to make transaction to call this function.

And now your question, "where it will cost gas even though it returns early?"

Then answer is YES.

Regardless how quick you return, all "send" or "write" transactions will cost gas.

Now, your title of this thread, "what is use of pure and view?"

well, both pure and view are for the "read" functions. And it will not cost any gas.

You can read more about that, here

Related Topic