โฉ ๐ ---๐จ๐ ๐ฐ๐๐๐๐๐๐๐๐๐๐ ๐ญ๐จ๐ช๐ป ๐๐๐๐๐ ๐บ๐๐๐๐ ๐ช๐๐๐๐๐๐๐ ๐ซ๐๐๐๐๐๐๐๐ ๐ด๐ผ๐บ๐ป ๐ฒ๐ต๐ถ๐พ--- ๐ โฉ
When it comes to ๐๐ฎ๐๐ฅ๐ข๐ & ๐๐ฑ๐ญ๐๐ซ๐ง๐๐ฅ visibility keywords,
๐๐ฆ๐ณ๐ฆ ๐ช๐ด ๐ธ๐ฉ๐ข๐ต M๐ฐ๐ด๐ต ๐ฐ๐ง ๐ถ๐ด A๐ญ๐ณ๐ฆ๐ข๐ฅ๐บ K๐ฏ๐ฐ๐ธ:
๐๐ฎ๐๐ฅ๐ข๐ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: Can be called from everywhere, internally as well as from outside the Contract
๐๐ฑ๐ญ๐๐ซ๐ง๐๐ฅ ๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: Can only be called from outside the contract and not accessible from within the contract.
๐๐ฆ๐ณ๐ฆ ๐ช๐ด ๐ธ๐ฉ๐ข๐ต S๐ฐ๐ฎ๐ฆ ๐ฐ๐ง ๐ถ๐ด M๐ช๐จ๐ฉ๐ต K๐ฏ๐ฐ๐ธ:
๐๐ฎ๐๐ฅ๐ข๐ ๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: Leads to Higher GAS COSTS
๐๐ฑ๐ญ๐๐ซ๐ง๐๐ฅ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: Costs Comparatively Lower GAS
๐ต๐๐, ๐ฏ๐๐๐ ๐๐ ๐๐๐๐ ๐ด๐๐๐ ๐๐ ๐๐ ๐ด๐๐๐๐ ๐ต๐ถ๐ป ๐ฒ๐ต๐ถ๐พ:
๐๐ก๐ฒ ๐๐ฑ๐๐๐ญ๐ฅ๐ฒ ๐๐จ๐๐ฌ ๐๐๐๐๐๐ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง๐ฌ ๐๐๐๐๐ ๐ฆ๐จ๐ซ๐ ๐๐๐ฌ ๐ญ๐ก๐๐ง ๐๐๐๐๐๐๐๐ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง๐ฌ?
In the case of PUBLIC Functions, arguments of the Functions are copied to ๐๐๐๐๐๐.
While on the other hand, Functions with External visibility can directly read arguments from ๐๐๐๐๐๐๐๐.
๐๐๐ฃ๐๐ ๐พ๐ผ๐๐๐ฟ๐ผ๐๐ผ ๐๐จ ๐พ๐๐๐ผ๐๐๐ ๐ฉ๐๐๐ฃ ๐๐๐๐๐๐, ๐๐ญ๐ฉ๐๐ง๐ฃ๐๐ก ๐๐ช๐ฃ๐๐ฉ๐๐ค๐ฃ๐จ ๐ง๐๐จ๐ช๐ก๐ฉ ๐๐ฃ ๐ ๐ก๐ค๐ฌ๐๐ง ๐๐ญ๐๐๐ช๐ฉ๐๐ค๐ฃ ๐พ๐ค๐จ๐ฉ(๐๐๐จ) ๐ฉ๐๐๐ฃ ๐๐๐ฝ๐๐๐พ ๐๐ช๐ฃ๐๐ฉ๐๐ค๐ฃ๐จ.
๐๐ณ๐ฆ ๐บ๐ฐ๐ถ ๐ค๐ถ๐ณ๐ช๐ฐ๐ถ๐ด ๐ข๐ฃ๐ฐ๐ถ๐ต ๐ธ๐ฉ๐บ ๐๐๐๐๐๐ ๐ง๐ถ๐ฏ๐ค๐ต๐ช๐ฐ๐ฏ๐ด ๐ค๐ฐ๐ฑ๐บ ๐ข๐ณ๐จ๐ถ๐ฎ๐ฆ๐ฏ๐ต๐ด ๐ต๐ฐ ๐๐๐๐๐๐ ๐ธ๐ฉ๐ช๐ญ๐ฆ ๐๐น๐ต๐ฆ๐ณ๐ฏ๐ข๐ญ ๐๐ถ๐ฏ๐ค๐ต๐ช๐ฐ๐ฏ๐ด ๐ฅ๐ฐ๐ฏ'๐ต?
PUBLIC Functions can be called from outside as well as within the contract(Internal Calls). And internal calls are executed via code JUMP as array arguments are passed internally by Pointers to Memory.
Hence, when the compiler generates OPCODES for an internal function, the function expects its arguments to be located in Memory itself.
However, this is not at all the case in EXTERNAL functions. They don't care about Internal Calls at all and thus end up saving some gas.
Don't forget to COMMENT and Share your IDEAS. ๐๐ฒ๐'๐ ๐๐ฒ๐ฎ๐ฟ๐ป ๐ฎ๐ป๐ฑ ๐๐ฟ๐ผ๐ ๐ง๐ผ๐ด๐ฒ๐๐ต๐ฒ๐ฟ.
Top comments (3)
Awesome article, thanks for the concise and detailed explanation, I find this taking into account the gas usage of different approaches to be one of the more fascinating aspects of Solidity
Thanks. Glad it was of some help
Thanks @toralero