Open In App

Function Visibility Specifier in Solidity

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.  

How do Function Visibility Modifiers and Inheritance Work Together?

Inheritance is the property in which the child contract uses the features of the parent contract. In Solidity, the keyword ‘is’ is used to provide inheritance. The function visibility is set in the parent contract according to which the child contract can use its parent’s function. The function visibility of the function provides the accessibility of the function to the child contract. The visibility defined in the parent contract is used to identify whether the child contract can use the function or not or if it is private to the parent contract only.

The four function visibility modifiers are: public, private, internal, and external. The accessibility of the function by child contract is defined by the following table:

Parent Function Visibility Specifier Child Function Accessibility
public Can access the function
private Cannot access the function
internal Can access the function
external Cannot access the function

Below is the Solidity program to implement the function visibility modifiers:

Solidity




// Solidity program to implement 
// the function visibility modifiers
  
// 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 or 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;
  }    
}


Function Visibility Modifier and Inheritance

 

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


Public function visibility modifier

 

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


Internal Function Visibility Modifier

 

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


External Function Visibility Modifier

 

Function Visibility Modifier vs State Variable Visibility Modifier

The main difference between the function and state variable visibility modifier is that there are four function visibility specifiers whereas three state variable visibility specifiers. The function visibility specifiers are public, private, internal, and external but state variable visibility specifiers are public, private, and internal only. If the state variable visibility specifier is public then it can be accessed by all the contracts, if private then the contract in which the state variable is declared can access it and if internal then the contract declared it and its child contract can access it.

Visibility Specifier Function State variable
Public All contracts can call the function with public visibility specifier. The State variable declared with the public visibility Specifier can be accessed by all the contracts.
Private Only the contract containing the function can call the function with visibility Specifier private. The State variables declared with private visibility Specifier can only be used by the same contract in which it is declared.
Internal The contract containing the function and its child contracts can call the function with internal visibility Specifier. The State variable declared with internal visibility Specifier can be used by the same contract or its child contracts.
External The external contracts can call the function with external visibility Specifier. The state variable does not have external visibility Specifier.
Default visibility Specifier  The default visibility Specifier for functions is public in Solidity. The default visibility Specifier for the State variable is internal in Solidity.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads