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. 𝗟𝗲𝘁'𝘀 𝗟𝗲𝗮𝗿𝗻 𝗮𝗻𝗱 𝗚𝗿𝗼𝘄 𝗧𝗼𝗴𝗲𝘁𝗵𝗲𝗿.

6