Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 25, 2022 04:01 pm GMT

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;    }}

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;    }}

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

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

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.

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.

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:

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


Original Link: https://dev.to/seek4samurai/function-visibility-in-solidity-smart-contracts-3fhf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To