# does it cost gas when executing view or pure function in solidity?

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.

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.
• 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