Function Visibility Specifier in Solidity
Last Updated :
24 Apr, 2025
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 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 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. |
Similar Reads
Solidity - Function Modifiers
Function behavior can be changed using function modifiers. Function modifier can be used to automatically check the condition prior to executing the function. These can be created for many different use cases. Function modifier can be executed before or after the function executes its code. The modi
8 min read
Solidity - Functions
A function is basically a group of code that can be reused anywhere in the program, which generally saves the excessive use of memory and decreases the runtime of the program. Creating a function reduces the need of writing the same code over and over again. With the help of functions, a program can
4 min read
Solidity Function Overloading
Function overloading in Solidity lets you specify numerous functions with the same name but varying argument types and numbers.Solidity searches for a function with the same name and parameter types when you call a function with certain parameters. Calls the matching function. Compilation errors occ
1 min read
Returning Values Functions in LISP
In the previous article on the LISP function, we have seen the syntax of defining a function and calling the function by passing some arguments. Returning values from a function: In LISP, whatever is the last expression in the body of a function becomes the return value of that function. Example: Le
2 min read
Functions in LISP
A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs. Defining Functio
3 min read
Function Pointer in C++
Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
Math Functions in PL/SQL
In PL/SQL, mathematical functions play a important role in performing calculations and manipulating numeric data. These functions allow us to execute a wide range of mathematical operations from basic arithmetic to complex computations within our PL/SQL code.In this article, we will learn about Math
4 min read
TypeScript Functions Type
TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error
6 min read
What is the Function type in TypeScript ?
TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance. It uses type
3 min read
If Statement in Solidity
If statement is a type of conditional statement. It is used to execute a certain block of code or statements only if a certain condition is true else no statement is executed. Syntax: if (condition) { // code is executed if condition is true } Here, if: keyword used to initiate the conditional state
2 min read