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 com…


This content originally appeared on DEV Community and was authored by Gourav Singh Rawat

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

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.

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 :)


This content originally appeared on DEV Community and was authored by Gourav Singh Rawat


Print Share Comment Cite Upload Translate Updates
APA

Gourav Singh Rawat | Sciencx (2022-06-25T16:01:48+00:00) Function visibility in Solidity Smart Contracts. Retrieved from https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/

MLA
" » Function visibility in Solidity Smart Contracts." Gourav Singh Rawat | Sciencx - Saturday June 25, 2022, https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/
HARVARD
Gourav Singh Rawat | Sciencx Saturday June 25, 2022 » Function visibility in Solidity Smart Contracts., viewed ,<https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/>
VANCOUVER
Gourav Singh Rawat | Sciencx - » Function visibility in Solidity Smart Contracts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/
CHICAGO
" » Function visibility in Solidity Smart Contracts." Gourav Singh Rawat | Sciencx - Accessed . https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/
IEEE
" » Function visibility in Solidity Smart Contracts." Gourav Singh Rawat | Sciencx [Online]. Available: https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/. [Accessed: ]
rf:citation
» Function visibility in Solidity Smart Contracts | Gourav Singh Rawat | Sciencx | https://www.scien.cx/2022/06/25/function-visibility-in-solidity-smart-contracts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.