22

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?
1
  • 2
    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.
    Commented Jul 7, 2018 at 18:22

2 Answers 2

27

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 its current state 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 number of EVM opcodes executed while completing it, so executing that pure function is also within that set of opcodes. That's why it's said that it costs gas.

Consider the 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 addThreeNumbers won't cost gas even if it called addNumbers internally since no transaction is needed throughout the function call.

5
  • 2
    Thanks a lot. I didn't get why internal calls always cost gas? Commented Jul 7, 2018 at 17:47
  • 2
    but according to the quote in the question, it should cost gas when using 'addNumbers'? Why is it so? Commented Jul 7, 2018 at 17:50
  • 2
    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? Commented Jul 7, 2018 at 17:53
  • 2
    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? Commented Jul 7, 2018 at 18:19
  • 2
    if you are calling updatestate you can't stop sate =c being executing. I edited the example, check whether it makes sense? Commented Jul 7, 2018 at 18:42
13

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.

4
  • 1
    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? Commented Jul 7, 2018 at 17:48
  • 3
    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.
    – user19510
    Commented Jul 7, 2018 at 17:55
  • 1
    That means internal calls always broadcast a transaction, as they cost? Commented Jul 7, 2018 at 18:14
  • 4
    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.
    – user19510
    Commented Jul 7, 2018 at 18:19

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.