View/Pure Gas usage - Cost gas if called internally by another function?

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. Jul 7, 2018 at 18:22

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.

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

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