1

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
  values.push(1);
  values.push(2);
  values.push(3);
  // 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

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.

  • 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? – Giorgi Lagidze Aug 24 '18 at 18:05
  • 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. – AnAllergyToAnalogy Aug 24 '18 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.