0

Noticed that I can not specify storage parameters in public functions but can do it in internal. I don't completely understand the reason of this behaviour. Could you explain it or share some useful links? Thanks in advance!

1

1 Answer 1

1

Noticed that I can not specify storage parameters in public functions but can do it in internal

Not necessarily true, storage parameter is only for array, struct or mapping types, not necessarily for internal functions.

The following code fails to compile (even if it has internal visibility):

pragma solidity 0.8.17;

contract Test {
    function add(uint storage a, uint storage b) internal returns (uint) {
        return a + b;
    }
}
3
  • Thank you for the clarification @Yongjian P. but what is the reason?
    – AGJoYy
    Nov 14, 2022 at 7:56
  • Because "storage" location is only relevant for reference types like arrays, structs, and mappings, uints are always passed by value, so you cannot hold a reference to a uint in storage. Nov 14, 2022 at 8:06
  • 1
    For your case of why storage parameters (e.g. from arrays, structs, and mappings) cannot be specified in public functions is that storage parameters are not directly modifiable. Parameters passed to public functions are usually modifiable and passed to the contract via memory and are copied in memory if needed. This case does not necessarily apply for internal functions. Hence, you can specify storage parameters in internal functions but not in public functions Nov 14, 2022 at 8:51

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.