Function Visibility Modifiers
The function visibility specifier helps in specifying the type of visibility of the function. It is like access specifiers in other OOP languages such as C++. The function visibility specifiers define the visibility of the function within the entire smart contract. There are four function visibility specifiers in Solidity: public, internal, external, and private.
1. Public
If the function visibility specifier is public then, the function can be used by all contracts on the blockchain. The public functions can be called by any contract in the blockchain. It is a simple and flexible visibility specifier in Solidity. It uses jump codes to reach the function when the function is called by contracts.
Syntax:
function function_name (parameter list) public modifier returns (return_type)
Below is the Solidity program to implement the public function visibility modifier:
Solidity
// Solidity program to implement the // public function visibility modifier // SPDX-License-Identifier: MIT pragma solidity ^0.5.0; contract A { function funpublic () public view returns (uint p) { // This function cna be used by any contract uint p = 10; return p; } } |
2. Private
If the function visibility specifier is private then, the function can be only used by the contract in which the function resides. The external contracts cannot call the private function. These functions can be called by higher specifier functions.
Syntax:
function function_name (parameter list) private modifier returns (return_type)
Below is the Solidity program to implement the private function visibility modifier:
Solidity
// Solidity program to implement the // private function visibility modifier // SPDX-License-Identifier: MIT pragma solidity ^0.5.0; contract A { uint p; function funprivate () private { //This function can only be used by contract A p = 10; } function f() public returns (uint) { funprivate(); return p; } } contract B{ uint q; A a = new A(); function fun() public returns(uint) { q = a.f(); return q; } } |
3. Internal
If the function visibility specifier is internal then, the function can be used by the contract in which the function is present or by its inherited contracts. These functions can be called by the inherited contracts only. The other contracts cannot call the internal functions.
Syntax:
function function_name (parameter list) internal modifier returns (return_type)
Below is the Solidity program to implement the internal function visibility modifier:
Solidity
// Solidity program to implement the // internal function visibility modifier // SPDX-License-Identifier: MIT pragma solidity ^0.5.0; contract A { public uint p = 10; function Functionint ()internal pure returns (uint p) { // This function is internal and can be used // by same contract ir child contract. return p; } } contract B is A{ function funint() public returns (uint) { // Internal function of parent return Functionint(); } } contract P{ B b = new B(); uint a; function f() public returns (uint) { a = b.funint(); return a; } } |
4. External
If the function visibility specifier is external then, the function can be used by the external contracts only. These functions are declared with external keywords which restricts the current contract to call the external function. The external contract does not contain the function it can call the external function in another contract.
Syntax:
function function_name (parameter list) external modifier returns (return_type)
Below is the Solidity program to implement the external function visibility modifier:
Solidity
// Solidity program to implement the // external function visibility modifier // SPDX-License-Identifier: MIT pragma solidity ^0.5.0; contract ABC { function externalfunction() external pure returns(uint) { // This external function can be used // by any external contract return 10; } } // External contract contract P{ ABC a = new ABC(); function f() public view returns(uint){ a.externalfunction(); } } |
Function Visibility Specifier in Solidity
Function in Solidity is a set of code that performs a specific task. The function provides reusability of code in smart contracts. In Solidity, functions are defined using the function keyword. The syntax of the function definition in Solidity is:
function function_name (parameter list) visibilty_specifier modifier returns (return_type)
{
Block of Code
}
Function visibility in Solidity defines the visibility of the function to other functions within the contract, or in other contracts. Function visibility helps to identify where in the contract or in other contracts, functions can be used. It is specified by the developer of the smart contract. The default variable visibility in Solidity is internal. The default function visibility in Solidity is public.