I'm aware of the fact that pure/view functions cost no gas with a simple "call". But what happens if i have a function that changes the contract state based on the pure/view function result. Do i have to pay gas for the pure/view functions looping if i do a "send"?

In my example I want to find a number in an array and return the index.

function indexOfUint(uint256[] _array, uint _value) public pure returns (uint256) {
    bool exist;
    for (uint i=0; i<_array.length;i++){
        if(_array[i] == _value){
            exist = true;
            return i;
    assert(exist == true);

4 Answers 4


There are two statements in https://github.com/ethereum/solidity/issues/992

the keyword view is introduced for functions (it replaces constant). Calling a view cannot alter the behavior of future interactions with any contract. This means such functions cannot use SSTORE, cannot send or receive ether and can only call other view or pure functions.

the keyword pure is introduced for functions, they are view functions with the additional restriction that their value only depends on the function arguments. This means they cannot use SSTORE, SLOAD, cannot send or receive ether, cannot use msg or block and can only call other pure functions.

It means view and pure can't change state of contract. If you need to change state of contract you need to remove pure, view keywords.

function indexOfUint(uint256[] _array, uint _value) public pure returns (uint256) {
    bool exist;

The function does not change state of contract. exist variable stores in stack


Pure and view functions do not consume gas when these are executed in your local node (or the node you are using as local, infura, etherscan, etc.). in such cases the transaction will not be sent.

On the other side, if the transaction is sent to the network (as in your case) pure and view functions will consume gas.

Hope this helps.


For calls, gas is charged only when called by a contract.

When you call a view function, it is executed locally. the transaction is not broadcast. it is free.

But when the view function is called by a contract. it is executed by others, the transaction may be recorded on blockchain.


Yes, if your function changes state, it will definitely use gas.

Please refer this: http://ethdocs.org/en/latest/contracts-and-transactions/account-types-gas-and-transactions.html#estimating-transaction-costs

A more detailed gas cost analysis based on what kind of operation it is: https://docs.google.com/spreadsheets/d/1m89CVujrQe5LAFJ8-YAUCcNK950dUzMQPMJBxRtGCqs/edit#gid=0

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.