Function visibility in Solidity Smart Contracts
Tags: beginners, tutorial, blockchain, programming
Published: 2022-06-25 16:01:48

Before we begin, I hope you're familiar with Solidity Smart contracts if not [check out
](https://dev.to/seek4samurai/guide-for-smart-contracts-in-solidity-56l4)
## What are Functions in Solidity

Functions can be declared both inside and outside of the contract in solidity. Functions are one of the most important component in a programming language. They are units or a block of code which you can call anytime or anywhere depending on it's visibility. 
```solidity
contract myContract{
    function add(uint num_1, uint num_2)public pure returns(uint){
        return num_1 + num_2;
    }
}
```

I mentioned visibility above because calling a function completely depends on it.
There are 4 types of visibility options of functions in solidity.

## Public functions
The example of function above was a public function, check this out.
```solidity
contract myContract{
    function add(uint num_1, uint num_2)public pure returns(uint){
        return num_1 + num_2;
    }
}
```
A public function is callable almost everywhere and it'll be a part of your contract UI, you can play with solidity on [Remix IDE](https://remix.ethereum.org/). The compiler automatically creates a getter function for public functions, which are visible after you compile them. You can learn more about [Getter functions here](https://docs.soliditylang.org/en/v0.8.15/contracts.html#getter-functions).

## Private functions
Private functions are functions that can only be called in the contract they are defined in. Private functions won't inherit at all, even if you derive another contract from your previous contract. 
Check this example.
```solidity
contract oldContract{
    function add(uint num_1, uint num_2)private pure returns(uint){
        return num_1 + num_2;
    }
}

// this is not gonna work coz the function add() is private

contract newContract is oldContract{
    uint ans = add(2,4);
}
```

## Internal functions
Functions that are internal can be called inside the contract they're declared in as well as in any other contract that are derived from that i.e. Internal functions are inherited unlike private functions.
```solidity
contract oldContract{
    function add(uint num_1, uint num_2)internal pure returns(uint){
        return num_1 + num_2;
    }
}

// this is gonna work coz the function add() is internal

contract newContract is oldContract{
    uint ans = add(2,4);
}
```

## External functions 
Functions that are set to external visibility can only be called from outside of the contract they are defined in.
You cannot call a external function within the same contract.
```solidity
contract oldContract{
    function add(uint num_1, uint num_2)external pure returns(uint){
        return num_1 + num_2;
    }
    // This won't work coz add() function is set external visibility
    uint public answer = add(2, 4);
}
```

To make that work, you can create another contract and then call that function over there. 
```solidity
contract oldContract{
    function add(uint num_1, uint num_2)external pure returns(uint){
        return num_1 + num_2;
    }
}
```
But first you'll need to make an instance of that old contract in your new contract.

```solidity
contract newContract {
    oldContract instance = new oldContract();
    uint public answer = instance.add(3, 4);
}
```
Hope function visibility was clear now and this tutorial was helpful.
Please check out:
{% embed https://www.youtube.com/watch?v=Tjd_IwLVlgI %}

Join for more blockchain, ethereum and solidity smart contract content.
_Thanks for reading and watching :)_