DEV Community

Cover image for Function visibility in Solidity Smart Contracts
Gourav Singh Rawat
Gourav Singh Rawat

Posted on

Function visibility in Solidity Smart Contracts

Before we begin, I hope you're familiar with Solidity Smart contracts if not check out

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.

contract myContract{
    function add(uint num_1, uint num_2)public pure returns(uint){
        return num_1 + num_2;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

contract myContract{
    function add(uint num_1, uint num_2)public pure returns(uint){
        return num_1 + num_2;
    }
}
Enter fullscreen mode Exit fullscreen mode

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. 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.

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.

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);
}
Enter fullscreen mode Exit fullscreen mode

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.

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);
}
Enter fullscreen mode Exit fullscreen mode

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.

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);
}
Enter fullscreen mode Exit fullscreen mode

To make that work, you can create another contract and then call that function over there.

contract oldContract{
    function add(uint num_1, uint num_2)external pure returns(uint){
        return num_1 + num_2;
    }
}
Enter fullscreen mode Exit fullscreen mode

But first you'll need to make an instance of that old contract in your new contract.

contract newContract {
    oldContract instance = new oldContract();
    uint public answer = instance.add(3, 4);
}
Enter fullscreen mode Exit fullscreen mode

Hope function visibility was clear now and this tutorial was helpful.
Please check out:

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

Top comments (0)