DEV Community

Decipher with Zaryab
Decipher with Zaryab

Posted on • Edited on

Solidity Guide: Behind the Scenes of PUBLIC and EXTERNAL Visibility of Solidity

โœฉ ๐ŸŽ€ ---๐‘จ๐’ ๐‘ฐ๐’๐’•๐’†๐’“๐’†๐’”๐’•๐’Š๐’๐’ˆ ๐‘ญ๐‘จ๐‘ช๐‘ป ๐’†๐’—๐’†๐’“๐’š ๐‘บ๐’Ž๐’‚๐’“๐’• ๐‘ช๐’๐’๐’•๐’“๐’‚๐’„๐’• ๐‘ซ๐’†๐’—๐’†๐’๐’๐’‘๐’†๐’“ ๐‘ด๐‘ผ๐‘บ๐‘ป ๐‘ฒ๐‘ต๐‘ถ๐‘พ--- ๐ŸŽ€ โœฉ

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
Alt Text
๐‘ต๐’๐’˜, ๐‘ฏ๐’†๐’“๐’† ๐’Š๐’” ๐’˜๐’‰๐’‚๐’• ๐‘ด๐’๐’”๐’• ๐’๐’‡ ๐’–๐’” ๐‘ด๐’Š๐’ˆ๐’‰๐’• ๐‘ต๐‘ถ๐‘ป ๐‘ฒ๐‘ต๐‘ถ๐‘พ:

๐–๐ก๐ฒ ๐„๐ฑ๐š๐œ๐ญ๐ฅ๐ฒ ๐๐จ๐ž๐ฌ ๐๐”๐๐‹๐ˆ๐‚ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ ๐‚๐Ž๐’๐“๐’ ๐ฆ๐จ๐ซ๐ž ๐†๐š๐ฌ ๐ญ๐ก๐š๐ง ๐„๐—๐“๐„๐‘๐๐€๐‹ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ?
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)

Collapse
 
toralero profile image
Drigo Reis

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

Collapse
 
zaryab2000 profile image
Decipher with Zaryab

Thanks. Glad it was of some help

Collapse
 
zaryab2000 profile image
Decipher with Zaryab

Thanks @toralero