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!

AGJoYy
  • 416
  • 3
  • 12

1 Answers1

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; } }

Yongjian P.
  • 4,170
  • 1
  • 3
  • 10
  • Thank you for the clarification @Yongjian P. but what is the reason? – AGJoYy Nov 14 '22 at 07: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. – Yongjian P. Nov 14 '22 at 08: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 – Yongjian P. Nov 14 '22 at 08:51