Let's suppose i have the following code in ethereum's solidity function:

function getArray() external pure returns(uint[]) {
  // Instantiate a new array in memory with a length of 3
  uint[] memory values = new uint[](3);
  // Add some values to it
  // Return the array
  return values;

As it's pure, it doesn't even read data from storage, doesn't change anything in storage. So imagine I deployed this contract. then there're people who want to invoke this function from web3.js or remix for example. Will this function cost them gas? I've read that view functions and pure functions are the way that don't change the storage. so they're free (no gas). If users use above function, why is it free? i still make the calculation and save temprorary data to memory.

2 Answers 2


It depends,

If they call a pure function itself, like the one in your example, getArray(), then no, it won't cost anything.

However, if they call a function which isn't pure or view, and that function internally calls the getArray() function, then they will have to pay for the whole transaction.

  • 1
    Then who does the calculation? imagine the function I posted here, in there i have a for-each loop that goes from 0 to 1000000, and saves each i variable in an array that i made inside a function like a memory variable. If it's gonna cost 0 gas to anyone who calls it, who does the calculation when they call from web3 or remix?
    – Chemistry
    Aug 24, 2018 at 18:05
  • 2
    Whoever is calling the function does the calculation in that case. But when you execute a function that makes changes, it means it gets included in a block, which basically means every miner does that calculation when trying to include it in the block, which is why you have to pay (but only one lucky miner gets rewarded for it). When it comes to Web3 specifically, when calling pure or view functions, I think technically the provider does the calculations, which would probably either be Infura or your locally running node. Aug 24, 2018 at 18:21

Nothing is free. Processing instructions cost gas, just much much less than a transaction that changes state. It may not cost gas when the EVM is run in popular IDEs, which can lead to the false belief that pure functions cost nothing.

This basic function:

    function bigLoop(uint max) public pure returns (uint) {
      uint n;
      for(n = 0; n < max; n++) {
      return n;

called from a web3 client with max = 310000 hitting a local dev geth node (geth --dev) will throw an out of gas exception. If you throw this into the loop:

    function bigLoop(uint max) public pure returns (uint) {
      uint n;
      for(n = 0; n < max; n++) {
         bytes memory dummy = new bytes(256);
      return n;

then max = 11000 will throw out of gas.

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.