1

Please let me know to delete the question if it's already been asked.


I was told (by a colleague of mine) that adding more external function to a smart contract increases its gas. I tested (on remix and metamask) and it's true, for example when my contract has only one external function and one internal function the total gas is: 89941. But if I change the internal one to external function, (so two external functions) then the gas would be 99943.

Question: Why does a contract having more external function require more gas (when it's deployed)? Is it due to memory/storage allocation?

2
  • Is this about deploying the contract? Mar 15, 2018 at 19:32
  • @cleanunicorn yes that's right. I've edited my question accordingly.
    – Aydin
    Mar 15, 2018 at 20:09

1 Answer 1

1

For each public or external function the contract has to have code that accept a calls to the function. For internal or private function no code is generated (you cannot call to them from other contracts).

If you use external in a function it will force the function to always use a external call when you use the function (ie CALL opcode). Usually the solidity compiler will make function calls to be jumps (ie JMP opcode).

2
  • Thank you very much for the answer. Am I right that, because for an external function some extra code is required and this occupies some storage, the external functions impose some costs on the contract?
    – Aydin
    Mar 16, 2018 at 10:03
  • Yes, an external and public functions requires some extra code for the contract to accepts calls to them.
    – Ismael
    Mar 16, 2018 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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