Skip to content
Home » Answers » Public vs External Functions

Public vs External Functions

Public vs External Functions

In the world of Solidity smart contracts, functions are the workhorses that define what your contract can do. But within these functions, there’s a hierarchy of access control, determining who can call them and from where. Today, we’ll explore the Public vs External Functions. These two important access specifiers dictate how users interact with your contract.

Imagine Your Contract as a Busy Office

Think of your smart contract as a bustling office. You have different departments handling specific tasks, represented by functions like processPayment, updateInventory, and generateReport. Now, how you control access to these departments determines how information flows:

  • Public Functions: These are like the reception area of your office. Anyone can walk in and interact with these functions, potentially triggering actions like making payments (processPayment).
  • External Functions: 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 (generateReport).

Understanding Public vs External Functions

Here’s a deeper dive into the specific characteristics of public and external functions:

  • Public Functions: These functions are the most accessible. They can be called from anywhere:
    • Directly within the contract itself.
    • From other contracts interacting with your contract.
    • Through external tools or user interfaces that can interact with the blockchain.
  • External Functions: These functions are designed for external interaction only. They cannot be called from within the same contract:
    • They are specifically meant to be invoked by other contracts.
    • They cannot be used by other functions within your contract.

🔥 Check this course out: Build a Semi-Fungible ERC404 Tokens’ Marketplace

Why Use External Functions?

There are a few reasons why you might choose to use external functions over public functions:

  • Reduced Gas Costs: 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), external functions can be slightly more gas-efficient than public functions. This is because public functions involve copying data into memory during the call, whereas external functions read data directly from calldata, which is a cheaper operation.
  • Separation of Concerns: By using external functions, you can clearly define functionalities that are meant for external interaction. This improves code readability and maintainability.
  • Security Considerations: In some cases, you might want to restrict internal access to certain functions to prevent accidental or unintended modifications.

Example: Public Payment Processing vs. External Reporting

Let’s see an example to illustrate the difference:

contract OnlineStore {
  // Public function for anyone to process a payment
  function processPayment() public payable {
    // Update inventory and send funds (implementation details)

  // External function for generating reports (only accessible from other contracts)
  function generateReport(address recipient) external {
    // Compile a report and send it to the recipient contract (implementation details)

In this example:

  • The processPayment function is public. Anyone can call this function to initiate a payment, potentially through a user interface or another contract.
  • The generateReport function is external. This function can only be called by another contract. It’s designed to be used by an external reporting service or another contract that needs to access your store’s data.


Choosing the right visibility for your functions is crucial for security, gas efficiency, and code organization. Here are some tips:

  • Use public functions for actions that anyone should be able to initiate.
  • Use external functions for functionalities specifically designed for external interaction between contracts.
  • Consider gas costs and code clarity when making the decision between public and external.

By effectively using public and external 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.

Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.

Follow us on –

🔮Twitter –

🔗LinkedIn –