# Solidity function visibility modifiers 101

By [Marco Besier](https://paragraph.com/@marcobesier) · 2023-03-04

---

Solidity functions have four different visibility modifiers that determine where and how they can be called. The four visibility modifiers are `public`, `private`, `internal`, and `external`. In this blog post, we'll explore each of these modifiers in detail.

Public functions
----------------

A `public` function is visible to all other contracts and can be called both internally and externally. Here's an example:

    contract MyContract {
      uint public myNumber;
    
      function setNumber(uint _number) public {
        myNumber = _number;
      }
    }
    

In this example, the `setNumber` function is marked as `public`, which means it can be called both internally and externally. The function takes a single parameter `_number` and sets the `myNumber` variable to its value.

Private functions
-----------------

A `private` function is only visible within the contract that defines it. Here's an example:

    contract MyContract {
      uint private myNumber;
    
      function _setNumber(uint _number) private {
        myNumber = _number;
      }
    
      function getNumber() public view returns (uint) {
        return myNumber;
      }
    }
    

In this example, the `_setNumber` function is marked as `private`, which means it can only be called within the same contract. The function takes a single parameter `_number` and sets the `myNumber` variable to its value. The `getNumber` function is marked as `public` and can be called both internally and externally to retrieve the value of `myNumber`.

Internal functions
------------------

An `internal` function is visible to the contract that defines it and any contracts that inherit from it. Here's an example:

    contract MyContract {
      uint internal myNumber;
    
      function _setNumber(uint _number) internal {
        myNumber = _number;
      }
    }
    
    contract MyDerivedContract is MyContract {
      function setNumber(uint _number) public {
        _setNumber(_number);
      }
    
      function getNumber() public view returns (uint) {
        return myNumber;
      }
    }
    

In this example, the `_setNumber` function is marked as `internal`, which means it can be called within the same contract and any contracts that inherit from it. The `MyDerivedContract` contract inherits from `MyContract`, so it can call the `_setNumber` function. The `setNumber` function is marked as `public` and calls the `_setNumber` function to set the value of `myNumber`. The `getNumber` function is also marked as `public` and can be called both internally and externally to retrieve the value of `myNumber`.

External functions
------------------

An `external` function is only visible externally and can only be called externally. Here's an example:

    contract MyContract {
      function sayHello(string memory _name) external pure returns (string memory) {
        return string(abi.encodePacked("Hello, ", _name, "!"));
      }
    }
    

In this example, the `sayHello` function is marked as `external`, which means it can only be called externally. The function takes a single parameter `_name` and returns a string that says "Hello, \[name\]!" using the `abi.encodePacked` function to concatenate the strings. The function is also marked as `pure`, which means it doesn't read or modify the state of the contract.

Conclusion
----------

Solidity function visibility modifiers are an important aspect of writing secure and efficient smart contracts. Understanding the differences between `public`, `private`, `internal`, and `external` functions is crucial if you want to control where and how your contract’s functions can be called.

Ready to master Web3 development?
---------------------------------

To become a skilled Web3 developer and master blockchain development, I invite you to join the Web3 Developer Academy. Inside the free membership, you’ll get free access to our “Intro to Web3 Development” video course and a global community of 100+ aspiring Web3 developers like you. [Take your blockchain development skills to the next level by joining the Web3 Developer Academy today.](https://membership.marcobesier.com)

---

*Originally published on [Marco Besier](https://paragraph.com/@marcobesier/solidity-function-visibility-modifiers-101)*
