{"id":7503,"date":"2024-03-26T08:42:22","date_gmt":"2024-03-26T08:42:22","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7503"},"modified":"2024-03-26T08:42:27","modified_gmt":"2024-03-26T08:42:27","slug":"public-vs-external-functions","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/","title":{"rendered":"Public vs External Functions"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/#Imagine_Your_Contract_as_a_Busy_Office\" title=\"Imagine Your Contract as a Busy Office\">Imagine Your Contract as a Busy Office<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/#Understanding_Public_vs_External_Functions\" title=\"Understanding Public vs External Functions\">Understanding Public vs External Functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/#Why_Use_External_Functions\" title=\"Why Use External Functions?\">Why Use External Functions?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/#Example_Public_Payment_Processing_vs_External_Reporting\" title=\"Example: Public Payment Processing vs. External Reporting\">Example: Public Payment Processing vs. External Reporting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/public-vs-external-functions\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In the world of Solidity smart contracts, functions are the workhorses that define what your contract can do. But within these functions, there&#8217;s a hierarchy of access control, determining who can call them and from where. Today, we&#8217;ll explore the Public vs External Functions. These two important access specifiers dictate how users interact with your contract.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"imagine-your-contract-as-a-busy-office\"><span class=\"ez-toc-section\" id=\"Imagine_Your_Contract_as_a_Busy_Office\"><\/span><strong>Imagine Your Contract as a Busy Office<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Think of your smart contract as a bustling office. You have different departments handling specific tasks, represented by functions like <code>processPayment<\/code>, <code>updateInventory<\/code>, and <code>generateReport<\/code>. Now, how you control access to these departments determines how information flows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Public Functions:<\/strong>&nbsp;These are like the reception area of your office. Anyone can walk in and interact with these functions, potentially triggering actions like making payments (<code>processPayment<\/code>).<\/li><li><strong>External Functions:<\/strong>&nbsp;Imagine these as specialized departments with restricted access. They can only be accessed from outside the office building (other contracts), but not directly from within the same office (other functions in the contract). Think of a department that requires special clearance or external communication to interact with (<code>generateReport<\/code>).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"understanding-public-vs-external-functions\"><span class=\"ez-toc-section\" id=\"Understanding_Public_vs_External_Functions\"><\/span><strong>Understanding Public vs External Functions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here&#8217;s a deeper dive into the specific characteristics of <code>public<\/code> and <code>external<\/code> functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Public Functions:<\/strong> These functions are the most accessible. They can be called from anywhere:<ul><li>Directly within the contract itself.<\/li><li>From other contracts interacting with your contract.<\/li><li>Through external tools or user interfaces that can interact with the blockchain.<\/li><\/ul><\/li><li><strong>External Functions:<\/strong> These functions are designed for external interaction only. They <strong>cannot<\/strong> be called from within the same contract:<ul><li>They are specifically meant to be invoked by other contracts.<\/li><li>They cannot be used by other functions within your contract.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/build-marketplace-erc404-tokens\" target=\"_blank\" rel=\"noreferrer noopener\">Build a Semi-Fungible ERC404 Tokens\u2019 Marketplace<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-use-external-functions\"><span class=\"ez-toc-section\" id=\"Why_Use_External_Functions\"><\/span><strong>Why Use External Functions?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are a few reasons why you might choose to use <code>external<\/code> functions over <code>public<\/code> functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Reduced Gas Costs:<\/strong> Calling a function within the same contract typically costs less gas compared to calling a function from another contract. However, in some specific scenarios (like large data arrays), <code>external<\/code> functions can be slightly more gas-efficient than <code>public<\/code> functions. This is because <code>public<\/code> functions involve copying data into memory during the call, whereas <code>external<\/code> functions read data directly from calldata, which is a cheaper operation.<\/li><li><strong>Separation of Concerns:<\/strong> By using <code>external<\/code> functions, you can clearly define functionalities that are meant for external interaction. This improves code readability and maintainability.<\/li><li><strong>Security Considerations:<\/strong> In some cases, you might want to restrict internal access to certain functions to prevent accidental or unintended modifications.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-public-payment-processing-vs-external-reporting\"><span class=\"ez-toc-section\" id=\"Example_Public_Payment_Processing_vs_External_Reporting\"><\/span><strong>Example: Public Payment Processing vs. External Reporting<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s see an example to illustrate the difference:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:16px\"><code>contract OnlineStore {\n  \/\/ Public function for anyone to process a payment\n  function processPayment() public payable {\n    \/\/ Update inventory and send funds (implementation details)\n  }\n\n  \/\/ External function for generating reports (only accessible from other contracts)\n  function generateReport(address recipient) external {\n    \/\/ Compile a report and send it to the recipient contract (implementation details)\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>processPayment<\/code>&nbsp;function is&nbsp;<code>public<\/code>. Anyone can call this function to initiate a payment, potentially through a user interface or another contract.<\/li><li>The&nbsp;<code>generateReport<\/code>&nbsp;function is&nbsp;<code>external<\/code>. This function can only be called by another contract. It&#8217;s designed to be used by an external reporting service or another contract that needs to access your store&#8217;s data.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Choosing the right visibility for your functions is crucial for security, gas efficiency, and code organization. Here are some tips:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use&nbsp;<code>public<\/code>&nbsp;functions for actions that anyone should be able to initiate.<\/li><li>Use&nbsp;<code>external<\/code>&nbsp;functions for functionalities specifically designed for external interaction between contracts.<\/li><li>Consider gas costs and code clarity when making the decision between&nbsp;<code>public<\/code>&nbsp;and&nbsp;<code>external<\/code>.<\/li><\/ul>\n\n\n\n<p>By effectively using <code>public<\/code> and <code>external<\/code> functions, you can create well-structured, secure, and efficient smart contracts that manage access control and optimize interactions within the complex world of blockchain technology.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013&nbsp;<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013&nbsp;<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":16,"featured_media":7504,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,46,29],"class_list":["post-7503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7503"}],"version-history":[{"count":1,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7503\/revisions"}],"predecessor-version":[{"id":7505,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7503\/revisions\/7505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7504"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}