Visibility Levels in Solidity

Visibility Levels in Solidity

Table of contents

No heading

No headings in the article.

The accessibility of functions and variables in a smart contract can be managed using one of five visibility levels in the Solidity programming language. These levels of visibility are public, external, internal, private, and view.

Public visibility describes a function or variable's ability to be called or read from outside the scope of the contract. Use the "public" keyword to designate a function or variable as public. For instance:

pragma solidity ^0.6.0;

contract MyContract {
    uint public myVariable;

    function myFunction() public {
        // function body
    }
}

Public visibility and external visibility are comparable, but only some functions can employ external visibility. However, internal calls from other functions inside the contract are not permitted when calling an external function from outside the contract. You can use the "external" keyword to designate a function as external. For instance:

pragma solidity ^0.6.0;

contract MyContract {
    function myExternalFunction() external {
        // function body
    }
}

Internal visibility denotes that a function or variable can only be invoked from within the scope of the contract and cannot be viewed by anybody external to it. Use the "internal" keyword to designate a function or variable as internal. For instance:

pragma solidity ^0.6.0;

contract MyContract {
    uint internal myVariable;

    function myFunction() internal {
        // function body
    }
}

Private visibility is only applicable to variables and functions created within contracts. Private functions and variables cannot be called or read by any other contract, even one that is descended from the one in which they are defined. They are only available within the contract in which they are defined. Use the "private" keyword to designate a function or variable as private. For instance:

pragma solidity ^0.6.0;

contract MyContract {
    uint private myVariable;

    function myFunction() private {
        // function body
    }
}

View visibility enables you to view the current state of the contract without changing it, and it can only be used for functions. The contract state cannot be changed by a view function, nor may it carry out any operations that alter the state, such as sending transactions or writing to storage. A function can be designated as a view by using the "view" keyword. For instance:

pragma solidity ^0.6.0;

contract MyContract {
    uint public myVariable;

    function myViewFunction() view returns (uint) {
        return myVariable;
    }
}

It is important to choose the appropriate visibility level for your functions and variables, as it helps to ensure the security and correct functioning of your smart contract. Public and external functions should be used carefully, as they can be called by anyone and may expose sensitive information or allow unintended actions to be performed on the contract. Internal and private functions and variables should be used for internal logic and implementation details that should not be exposed to external parties. View functions can be used to allow external parties to read the state of the contract without modifying it.

I hope this article has helped to clarify the differences between the visibility levels in Solidity.

Did you find this article valuable?

Support Maxwell Onyeka by becoming a sponsor. Any amount is appreciated!