1

Do I need to explicitly initialize the 'i' variable in a for loop? Since i is a uint and its default value is 0, why should I spend gas in a MSTORE operation to initialize the variable with 0? The same question applies to result.

function sum10() external pure returns (uint256) {
    uint256 result;
    for(uint256 i; i< 10; i++){
       result += i;
    }
    return result;
}

1 Answer 1

1

This kind of things are simply easiest to try out in something like Remix.

I tried all three variants: as-is, explicitly setting i and explicitly setting result . The first two cost the same (25315), and the last one costs 25323.

Honestly, I'm really not sure why it's like this.

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.