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!
Asked
Active
Viewed 42 times
0
-
1Check out the answer here: https://ethereum.stackexchange.com/a/107123/102055 explained here very well. – Ali Murtaza Nov 14 '22 at 08:00
1 Answers
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
-
-
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
-
1For 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