0

Does it cost more gas to call a function more than once in another function?

For example:

function getValue() public returns(uint){
    return 5;
}

function getOtherValue() public returns(uint){
    require(getValue() < 6, "Error:Too much");
    require(getValue() > 4, "Error:Too little");
    return getValue() -1;
}

Does this other getOtherValue() function below use less gas, the same gas or even more gas than the one above?

function getOtherValue() public returns(uint){
    uint gottenValue = getValue();
    require(gottenValue < 6, "Error:Too much");
    require(gottenValue > 4, "Error:Too little");
    return gottenValue -1;
}
6
  • 1
    if you optimize it, it will inline it and no additional cost. but if you call a function in another contract, the overhead will be about 9000 gas for CALL instruction
    – Nulik
    Jul 9, 2021 at 21:42
  • What do you mean by optimize it? All this functions are called within the same contract.
    – YulePale
    Jul 10, 2021 at 9:12
  • solc --optimize
    – Nulik
    Jul 10, 2021 at 10:40
  • Where should I run this command, I using remix to run these smart contracts.
    – YulePale
    Jul 10, 2021 at 11:12
  • 1
    i am not using remix, I am using solc and Golang
    – Nulik
    Jul 10, 2021 at 15:43

0

Your Answer

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