Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity

What is the visibility of functions in Solidity?

Fatima Numan

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Solidity is a widely used object-oriented programming language for designing smart blockchain contracts. It is built in a way that it applies business logic in its smart contracts while managing a chain of transactions.

Visibility in Solidity

Solidity offers programmers a way to select the visibility of functions and variables within a contract, depending on the user’s requirements.

It is similar to the approach followed by several object-oriented languages, where we define classes along their access permissions and dependencies.

Visibility of functions

The visibility of functions can be set in four ways in Solidity:

  • Public
  • Private
  • Internal
  • External

Let's discuss them in detail:

The Visibility states of functions in Solidity.

Public

A public function can be accessed by the contract containing it and all the other smart contracts outside it using Ethereum Virtual Machine (EVM) message calls.

Private

A private function is the one that can only be accessed by the contract it resides within. No other contract can access it.

Internal

The contract can only access an internal function it was declared in, or by the same contract’s derived contracts or subclasses.

External

An external function can only be called by the contracts other than the one it was declared inside, or the ones derived from its main contract. Only external parties can access such a function.

Note: The internal state is the default visibility state for functions if no visibility has been specified.

Syntax

To define the visibility for any variable, we use the following syntax:

function function_name(data_type parameter) visibility returns (data_type)
  • The function_name is the name of the function specified by the user.
  • The data_type can be int, uint, string, etc.
  • The parameter defines the parameters specified by the user. We can define as many parameters as we want for our function.
  • The visibility can be public, private, external, or internal.

Code

Now, let’s look at a code to better understand how the variables mentioned above are declared and accessed:

pragma solidity ^0.5.0;
/// defined contract Class A
contract Class_A{
/// internal member defined for Class A
int internal A1=1;
/// an internal function that returns the sum between two numbers a and b
function Add(int a, int b) internal pure returns (int){
return a + b;
}
/// a public function that returns a message specified by the user
function SayHello(bytes32 message) public pure returns(bytes32){
return message;
}
}
/// defined contract Class B
contract Class_B {
/// a private function that returns the difference of two numbers
function Subtract(int a, int b) private pure returns (int) {
return a-b;
}
/// a function that calls a public function from Class A
function Call_A_for_B() public returns (bytes32){
/// object of Class A created
Class_A Derived_A = new Class_A();
/// called the public function from Class A
bytes32 my_message = Derived_A.SayHello("Hello Mars!");
return my_message;
}
}
/// defined contract Class C as child of Class A
contract Class_C is Class_A {
/// a function that calls an internal function from Class A
function Call_A_for_C() public view returns (int){
/// used internal data member of Class A
int answer = Add(A1, 2);
return answer;
}
}
Demonstrating the visibility of functions in Solidity

Explanation

  • Line 1: We use pragma to specify the version of Solidity. We want to compile the code in pragma.
  • Line 7: We define an internal member A1 for Class_A.
  • Lines 10–12: We define an internal function to add two numbers in Class_A, where we use the pure keyword. This keyword specifies that no state variable can be modified using this function. This function has been used by the derived contract Class_C.
  • Lines 15–17: We define a public function to print a message in the contract Class_A that Class_B later on calls. This function also uses the pure keyword.
  • Lines 29–37: We define a function named Call_A_for_B() and call the public function defined in Class_A inside it, to return a message after creating an object of Class_A in line 23.
  • Line 41: We create a derived contract Class_C from Class_A using the keyword is. This contract is internal to Class_A.
  • Lines 43–48: We define a function named Call_A_for_C() and call the internal function defined in Class_A inside it, for addition where we pass the member A1 of Class_A as the argument. We use the view keyword for this function to ensure that our function reads the variable A1 but cannot change it.

RELATED TAGS

solidity

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring