Solidity – Why Do View/Pure Functions Need Gas?

gasgas-limitout-of-gasremixsolidity

pragma solidity ^0.4.11;

contract Test {
    function toBytes(address a) public pure returns (bytes b) {
        assembly {
            let m := mload(0x40)
            mstore(add(m, 20), xor(0x140000000000000000000000000000000000000000, a))
            mstore(0x40, add(m, 52))
            b := m
       }
    }

    function toBytes(uint _num) public pure returns (bytes _ret) {
        assembly {
            _ret := mload(0x10)
            mstore(_ret, 0x20)
            mstore(add(_ret, 0x20), _num)
        }
    }

    function test(uint num1, uint num2) public view returns (bytes) {
        bytes memory a1 = toBytes(msg.sender);
        bytes memory a2 = toBytes(num1);
        bytes memory a3 = toBytes(num2);
        bytes memory ret = new bytes(a1.length + a2.length + a3.length);
        uint x = 0;

        for (uint i = 0; i < a1.length; i++) {
            ret[x++] = a1[i];
        }

        for (i = 0; i < a2.length; i++) {
            ret[x++] = a2[i];
        }

        for (i = 0; i < a3.length; i++) {
            ret[x++] = a3[i];
        }

        return ret;
    }
}

I tried the code in Remix, and it always gives me an error:

call to browser/test.sol:Test.test errored: VM error: out of gas.
out of gas  The transaction ran out of gas. Please increase the Gas Limit.
        Debug the transaction to get more information.

, even if I modify the gas limit to a very large value.

I thought view/pure functions will not affect the blockchain, and thus they are not executed by miners. Therefore, they consume no gas.

Am I wrong? If I'm wrong, how to make the code good for execution?

Many thanks!

Best Answer

Yes, pure/view functions are bound to the same gas constraints than transactions. They need gas to be executed. The only difference is that the gas is not paid for.