5

I know similar questions have been asked before however I could not find an good explanation for the following,

In one of the tutorial it says:

Pure and View functions don't cost any gas to call if they're called externally from outside the contract. But they do cost gas if called internally by another function.

  • Why do they need gas if they don't update anything?
  • How does read cost gas?
  • Isn't it read from my local blockchain?
  • As I know any operation have a cost in Gas if executed on a Miners node, not on your local (user's) node. Read (from blockchain) ops have a smaller cost, Write - more. – Alex Koz. Jul 7 '18 at 18:22
4

As I understand,

Pure and View functions don't cost any gas to call if they're called externally from outside

In this type of case, there won't be any transaction initiated because this will be like just querying the blockchain for it's current satate and nothing will be changed.

But they do cost gas if called internally by another function.

This means there's already a transaction to change the state of the blockchain, and that process of changing state need to use that pure function, say for calculations. Gas cost for a transaction depends on the no of EVM opcodes executed while completing it. So the executing that pure function is also within that set of opcodes. That's why it's said that it costs gas.

consider following contract,

pragma solidity ^0.4.24;


contract PureFunctionTest {

   uint state;

    function addNumbers(uint a, uint b) public pure returns (uint) {
       return a +b ;
   }

   function updateState(uint a, uint b) public {

     uint c = addNumbers(a,b);
     state = c;
   }

   function addThreeNumbers(uint a, uint b, uint c) public pure returns (uint) {

       uint temp = addNumbers(a,b);
       uint num  = addNumbers(temp,c);
       return num;
   }
}

Just calling addNumbers won't cost anything. But calling updateState will cost including cost to addNumbers(a,b) as well. Calling addThreeNums won't cost gas even it called addNUmbers internally since no transaction is needed throughout the function call.

  • 1
    Thanks a lot. I didn't get why internal calls always cost gas? – Mustafa Caglar Jul 7 '18 at 17:47
  • 1
    but according to the quote in the question, it should cost gas when using 'addNumbers'? Why is it so? – Mustafa Caglar Jul 7 '18 at 17:50
  • 1
    not when only the addNUmbers called alsone. but when it's called inside other functions that change the states. because for a transaction cost what matters is the number of opcodes that get executed. do you get it? – Achala Dissanayake Jul 7 '18 at 17:53
  • 1
    What if I do not update the state? Let's say if I just call the function addNumbers and I dont use this line: state = c; but according to the quote in the question, this would still cost someting, as it is internal? – Mustafa Caglar Jul 7 '18 at 18:19
  • 1
    if you are calling updatestate you can't stop sate =c being executing. I edited the example, check whether it makes sense? – Achala Dissanayake Jul 7 '18 at 18:42
4

As the quote says, they don't cost anything if they're used to just read from your local node.

They do cost gas if they're part of a transaction that needs to be mined. Gas compensates the network for running your code. It doesn't matter that this particular function doesn't update state; it still takes resources to execute, so it costs gas.

  • Thanks a lot. Is internal calls are always part of transaction? What is the diffrence between the reading same variable from internal function and from external function? – Mustafa Caglar Jul 7 '18 at 17:48
  • 1
    Whether you pay gas is related to whether you broadcast a transaction or not. If you're not broadcasting a transaction, just doing a local read, then nothing costs anything. If you're broadcasting a transaction, everything costs. – smarx Jul 7 '18 at 17:55
  • That means internal calls always broadcast a transaction, as they cost? – Mustafa Caglar Jul 7 '18 at 18:14
  • 1
    No. Internal calls can be part of a transaction or not part of a transaction. The distinction between "external call" and "internal call" is orthogonal to whether or not something costs gas. – smarx Jul 7 '18 at 18:19

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.