[Ethereum] Gas cost call vs send for pure/view functions

gaspuresolidityview

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);
}

Best Answer

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