0
pragma solidity ^0.8.0;
contract convertingVariables{
    address private owner;
    address public owner_2;

    constructor(){
        owner= msg.sender;
        owner_2= owner;
    }

    function gettingPrivateVariable() public view returns(address){
        return owner;
    }
}

owner_2 takes 2514 gas (Cost only applies when called by a contract) gettingPrivateVariable() takes 2500 (Cost only applies when called by a contract)

Any reasons?

1 Answer 1

0

The small difference in gas cost (14 gas units) between using the getter function for owner_2 and calling gettingPrivateVariable() is probably due to how these two functions are implemented internally. The automatically generated getter function might have some optimizations that make it slightly more efficient than the explicitly defined function.

When it comes to gas cost, the variation between the automatically generated getter function for a public variable and a custom public function is minimal. While this optimization can help reduce gas consumption, the impact on overall gas usage may not be significant.

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.