Network
Launch Date
Consensus
Note
Sepolia
Oct 2021
PoW
Like-for-like representation of Ethereum
Görli
Jan 2019
PoA
Proof-of-Authority
Kiln
Mar 2022
PoS
Post-Merge (for ETH2), shadow fork of the mainnet
Kintsugi
Dec 2021
PoS
DEPRECATED, use Kiln; post-Merge (for ETH2)
Ropsten
Nov 2016
PoW
DEPRECATED, use Sepolia; the Merge to happen on Jun 8, 2022
Rinkeby
Apr 2017
PoA
DEPRECATED, use Görli and Görli Faucet
Kovan
Mar 2017
PoA
DEPRECATED, use Sepolia or Görli
List of active and deprecated Ethereum testnets, including Kintsugi.
Features
Optimistic rollup 
ZK-rollup 
Proof
Uses fraud proofs to prove transaction validity. 
Uses validity (zero-knowledge) proofs to prove transaction validity. 
Capital efficiency
Requires waiting through a 1-week delay (dispute period) before withdrawing funds. 
Users can withdraw funds immediately because validity proofs provide incontrovertible evidence of the authenticity of off-chain transactions. 
Data compression
Publishes full transaction data as calldata to Ethereum Mainnet, which increases rollup costs. 
Doesn't need to publish transaction data on Ethereum because ZK-SNARKs and ZK-STARKs already guarantee the accuracy of the rollup state. 
EVM compatibility
Uses a simulation of the Ethereum Virtual Machine (EVM), which allows it to run arbitrary logic and support smart contracts. 
Doesn't widely support EVM computation, although a few EVM-compatible ZK-rollups have appeared. 
Rollup costs
Reduces costs since it publishes minimal data on Ethereum and doesn't have to post proofs for transactions, except in special circumstances. 
Faces higher overhead from costs involved in generating and verifying proofs for every transaction block. ZK proofs require specialized, expensive hardware to create and have high on-chain verification costs. 
Trust assumptions
Doesn't require a trusted setup. 
Requires a trusted setup to work. 
Liveness requirements
Verifiers are needed to keep tabs on the actual rollup state and the one referenced in the state root to detect fraud. 
Users don't need someone to watch the L2 chain to detect fraud. 
Security properties 
Relies on cryptoeconomic incentives to assure users of rollup security. 
Relies on cryptographic guarantees for security. 
Start building
on Alchemy.
Sign up for free
Start building on Optimism.
Sign up for free
Start building on Arbitrum.
Sign up for free
Start building on Ethereum.
Sign up for free
Start building on Polygon.
Sign up for free
Start building on Starknet.
Sign up for free
Start building on Flow.
Sign up for free
kiln faucet
Get free Kiln ETH.
Start building today
Goerli faucet
Get free Goerli ETH.
Start building today
mumbai faucet
Get free Mumbai Matic.
Start building today
rinkeby faucet
Get free Rinkeby
ETH.
Start building today
Start building on Ethereum.
Get started for free
Start building on Ethereum.
Get started for free
Start building on Flow.
Get started for free
Start building on Polygon.
Get started for free
Start building on Starknet.
Get started for free
Start building on Optimism.
Get started for free
Start building on Solana.
Get started for free
Start building on Solana.
Sign up for beta access
Start building on Solana.
Join the waitlist
Arbitrum logo
Start building on Arbitrum.
Get started for free
Learn
Solidity at
Alchemy
University
Get started today
curl 
https://release.solana.com/v1.10.32/solana-install-init-x86_64-pc-windows-msvc.exe 
--output 
C:\solana-install-tmp\solana-install-init.exe 
--create-dirs
Learn Solidity
FUNCTION VISIBILITY OVERVIEW

What is Solidity function visibility?

Learn the Differences Between Public, External, Internal, and Private Function Visibility
Last Updated:
October 4, 2022
Table of Contents
Table of Contents
Table of Contents

{{learn-solidity}}

In Solidity smart contract programming, there is a provision that allows developers to decide who or what other smart contracts can call their functions based on specified restrictions. The ability to define how smart contract functions can be interacted with is known as function visibility. 

What is function visibility?

The function visibility feature in Solidity smart contracts is used to ensure that when functions are specified, their level of accessibility, including public, external, internal, and private, are maintained as they were intended by the developers.

If a developer doesn’t use the right function visibility type, or if no visibility model is specified in their code, the contract's default public visibility is exposed to potentially exploitable security vulnerabilities. Apart from the security implications of not having function visibility specified, smart contracts might not work as intended since the functions will be working without proper instructions. 

Who can call a smart contract function?

There are three kinds of contracts that can call a function: the main contract itself, a contract inherited from the main contract (DerivedContract), and a third-party contract (OutsideContract).

‍Calling a smart contract means to access the internal code that is in a smart contract and the data included in it. This is a way of retrieving data from the contract. 

1. The Main Contract (MainContract)

This is the syntax of the MainContract calling its function:



contract MainContract {
     uint x; 
     // someone external calls main 
      function main() external {
          // inside main, we call the private store function
	store(10);
      }

      function store(uint _x) public {
x = _x;
      }
}

2. A Contract Inherited from the Main Contract (DerivedContract)

This is the syntax of a DerivedContract that is using a derived function from the MainContract:



contract DerivedContract is MainContract {
      function newFunction() external {
          // call a function derived from the MainContract
          store(15);
     }
}

3. A 3rd-Party Contract (OutsideContract)

This is the syntax of an OutsideContract that is is calling the main contract from an outside function.



contract OutsideContract {
      function functionOutside(address mainContractAddress) external {
            // here we send a message call to main contract 
            MainContract(mainContractAddress).store(15);
      }
}

How do function visibility modifiers and inheritance work together?

Inheritance is when the contents of one contract are copied into another contract (a “derived contract”) by using the “is” keyword, and the function modifier visibility works with a derived child contract according to the type of visibility a contract is assigned.



Contract ParentContract {
    function performTask1() public {
            //performs the first task
              }
}
 
Contract ChildContract is ParentContract {
        	function performTask2() public {
            //performs a second task
              }
}

The relationship between function visibility modifiers and inheritance is explained below:

  • If the modifier of the ParentContract function is public, a ChildContract can access it.
  • If the modifier of the ParentContract function is internal, the ChildContract can access it.
  • If the modifier of the ParentContract function is private, the inheriting ChildContract cannot access it. 
  • If the modifier of the ParentContract function is external, the inheriting ChildContract cannot access it.

How can function visibility modifiers help gas optimization?

Because the parameters for external function visibility modifier are not saved to memory, but read directly from calldata, smart contracts consume less gas. In contrast public functions use input parameters are saved to memory, which costs more gas to deploy a smart contract.

What are function visibility modifiers?

There are four different types of function visibility modifiers that range from most accessible to least accessible: public, external, internal, and private. Developers should modify functions with the correct visibility based on who they want to be able to see and call the function.

1. Public

A public function can be accessed by any of the three types of calling contracts: main contract, derived contract, and a third party contract. A function is public by default.



contract RandomContract {
      function functionPublic() public {
            //performs a task
              }
}

As shown in the code sample above, any contract in the codebase can access the “functionPublic” function because of the “public” visibility.  

2. External

An external function is a function that can only be called by a third party. With the external function visibility, a contract that can call the function must be independent of the main contract and can not be a derived contract.



contract RandomContract {
      function externalFunction() external {
            //performs a task
      }
}

Above is a random contract (RandomContract) that is performing some task. The code snippet below is an external contract that is calling the external function on the RandomContract.



interface IRandomContract {
	function externalFunction() external;
}
contract CallingContract {
        	function functionIsExternalCall(address addr) external {
	IRandomContract(addr).externalFunction();
          }
}

3. Internal

An internal function can be called by the main contract and any of its derived contracts. Internal functions are accessible from the main contract in which they were initially declared and by the contracts that extend from this main contract through inheritance. 

4. Private

A private function can only be called by the main contract in which it was specified. Private functions are used initially according to common practice, but if the scope is wider than this modifier type, any other plausible modifier should be used.

What is the default function visibility in Solidity?

If the visibility modifier of a function is not explicitly declared in the code, the function is set to public visibility by default. Leaving the function visibility blank and relying on the Solidity compiler to use the default visibility is not a recommended best practice; intentionally setting the function visibility makes code more legible and easier to understand during code reviews.

What is the difference between function and state variable visibility modifiers?

The difference between function visibility modifiers and state variable visibility modifiers is that state variables do not have the external visibility modifier option. 

State variables are variables whose values are permanently stored in contract storage, which holds data persistently between function calls. Like function visibility modifiers, state variables also have visibility modifiers which are Public, Private and Internal. 

State Variable Visibility Modifiers

State variables are declared in the contract section of the program. The variable data type is specified and after that, the visibility modifier is assigned to the variable. 



Contract TestingVariableStorage {
      uint public publicData; // Publicly visible variable 
        	string private privateData; // Privately visible variable
        	string internal internalData; // Internally visible variable
}

1. Public

A state variable with the public modifier can be accessed by any contract in the application. If a variable is declared as public, the data stored in it can be read by the main contract, a derived contract, or an external contract. 

2. Internal

State variables declared with the internal modifier can only be accessed within the contract in which they were defined and by a derived contract. A third party or another external contract cannot access the storage of the data defined as an internal variable. 

3. Private

If a state variable is private, only the main contract in which they were declared can call them. The private visibility modifier restricts access from other parties apart from the main contract so that the data it holds is protected. 

What is the default variable visibility in Solidity?

When the variable visibility is not defined, the default value is internal. It is best practice to declare a variable as private until the scope widens.

ALCHEMY SUPERNODE - ETHEREUM NODE API

Scale to any size, without any errors

Alchemy Supernode finally makes it possible to scale blockchain applications without all the headaches. Plus, our legendary support will guide you every step of the way.

Get started for free
Supernode footer
Learn Solidity
FUNCTION VISIBILITY OVERVIEW

Learn Solidity: What is function visibility?

Learn the Differences Between Public, External, Internal, and Private Function Visibility
Last Updated:
October 4, 2022
Don't miss an update
Sign up for our newsletter to get alpha, key insights, and killer resources.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Table of Contents

In Solidity smart contract programming, there is a provision that allows developers to decide who or what other smart contracts can call their functions based on specified restrictions. The ability to define how smart contract functions can be interacted with is known as function visibility. 

What is function visibility?

The function visibility feature in Solidity smart contracts is used to ensure that when functions are specified, their level of accessibility, including public, external, internal, and private, are maintained as they were intended by the developers.

If a developer doesn’t use the right function visibility type, or if no visibility model is specified in their code, the contract's default public visibility is exposed to potentially exploitable security vulnerabilities. Apart from the security implications of not having function visibility specified, smart contracts might not work as intended since the functions will be working without proper instructions. 

Who can call a smart contract function?

There are three kinds of contracts that can call a function: the main contract itself, a contract inherited from the main contract (DerivedContract), and a third-party contract (OutsideContract).

‍Calling a smart contract means to access the internal code that is in a smart contract and the data included in it. This is a way of retrieving data from the contract. 

1. The Main Contract (MainContract)

This is the syntax of the MainContract calling its function:



contract MainContract {
     uint x; 
     // someone external calls main 
      function main() external {
          // inside main, we call the private store function
	store(10);
      }

      function store(uint _x) public {
x = _x;
      }
}

2. A Contract Inherited from the Main Contract (DerivedContract)

This is the syntax of a DerivedContract that is using a derived function from the MainContract:



contract DerivedContract is MainContract {
      function newFunction() external {
          // call a function derived from the MainContract
          store(15);
     }
}

3. A 3rd-Party Contract (OutsideContract)

This is the syntax of an OutsideContract that is is calling the main contract from an outside function.



contract OutsideContract {
      function functionOutside(address mainContractAddress) external {
            // here we send a message call to main contract 
            MainContract(mainContractAddress).store(15);
      }
}

How do function visibility modifiers and inheritance work together?

Inheritance is when the contents of one contract are copied into another contract (a “derived contract”) by using the “is” keyword, and the function modifier visibility works with a derived child contract according to the type of visibility a contract is assigned.



Contract ParentContract {
    function performTask1() public {
            //performs the first task
              }
}
 
Contract ChildContract is ParentContract {
        	function performTask2() public {
            //performs a second task
              }
}

The relationship between function visibility modifiers and inheritance is explained below:

  • If the modifier of the ParentContract function is public, a ChildContract can access it.
  • If the modifier of the ParentContract function is internal, the ChildContract can access it.
  • If the modifier of the ParentContract function is private, the inheriting ChildContract cannot access it. 
  • If the modifier of the ParentContract function is external, the inheriting ChildContract cannot access it.

How can function visibility modifiers help gas optimization?

Because the parameters for external function visibility modifier are not saved to memory, but read directly from calldata, smart contracts consume less gas. In contrast public functions use input parameters are saved to memory, which costs more gas to deploy a smart contract.

What are function visibility modifiers?

There are four different types of function visibility modifiers that range from most accessible to least accessible: public, external, internal, and private. Developers should modify functions with the correct visibility based on who they want to be able to see and call the function.

1. Public

A public function can be accessed by any of the three types of calling contracts: main contract, derived contract, and a third party contract. A function is public by default.



contract RandomContract {
      function functionPublic() public {
            //performs a task
              }
}

As shown in the code sample above, any contract in the codebase can access the “functionPublic” function because of the “public” visibility.  

2. External

An external function is a function that can only be called by a third party. With the external function visibility, a contract that can call the function must be independent of the main contract and can not be a derived contract.



contract RandomContract {
      function externalFunction() external {
            //performs a task
      }
}

Above is a random contract (RandomContract) that is performing some task. The code snippet below is an external contract that is calling the external function on the RandomContract.



interface IRandomContract {
	function externalFunction() external;
}
contract CallingContract {
        	function functionIsExternalCall(address addr) external {
	IRandomContract(addr).externalFunction();
          }
}

3. Internal

An internal function can be called by the main contract and any of its derived contracts. Internal functions are accessible from the main contract in which they were initially declared and by the contracts that extend from this main contract through inheritance. 

4. Private

A private function can only be called by the main contract in which it was specified. Private functions are used initially according to common practice, but if the scope is wider than this modifier type, any other plausible modifier should be used.

What is the default function visibility in Solidity?

If the visibility modifier of a function is not explicitly declared in the code, the function is set to public visibility by default. Leaving the function visibility blank and relying on the Solidity compiler to use the default visibility is not a recommended best practice; intentionally setting the function visibility makes code more legible and easier to understand during code reviews.

What is the difference between function and state variable visibility modifiers?

The difference between function visibility modifiers and state variable visibility modifiers is that state variables do not have the external visibility modifier option. 

State variables are variables whose values are permanently stored in contract storage, which holds data persistently between function calls. Like function visibility modifiers, state variables also have visibility modifiers which are Public, Private and Internal. 

State Variable Visibility Modifiers

State variables are declared in the contract section of the program. The variable data type is specified and after that, the visibility modifier is assigned to the variable. 



Contract TestingVariableStorage {
      uint public publicData; // Publicly visible variable 
        	string private privateData; // Privately visible variable
        	string internal internalData; // Internally visible variable
}

1. Public

A state variable with the public modifier can be accessed by any contract in the application. If a variable is declared as public, the data stored in it can be read by the main contract, a derived contract, or an external contract. 

2. Internal

State variables declared with the internal modifier can only be accessed within the contract in which they were defined and by a derived contract. A third party or another external contract cannot access the storage of the data defined as an internal variable. 

3. Private

If a state variable is private, only the main contract in which they were declared can call them. The private visibility modifier restricts access from other parties apart from the main contract so that the data it holds is protected. 

What is the default variable visibility in Solidity?

When the variable visibility is not defined, the default value is internal. It is best practice to declare a variable as private until the scope widens.

In Solidity smart contract programming, there is a provision that allows developers to decide who or what other smart contracts can call their functions based on specified restrictions. The ability to define how smart contract functions can be interacted with is known as function visibility. 

What is function visibility?

The function visibility feature in Solidity smart contracts is used to ensure that when functions are specified, their level of accessibility, including public, external, internal, and private, are maintained as they were intended by the developers.

If a developer doesn’t use the right function visibility type, or if no visibility model is specified in their code, the contract's default public visibility is exposed to potentially exploitable security vulnerabilities. Apart from the security implications of not having function visibility specified, smart contracts might not work as intended since the functions will be working without proper instructions. 

Who can call a smart contract function?

There are three kinds of contracts that can call a function: the main contract itself, a contract inherited from the main contract (DerivedContract), and a third-party contract (OutsideContract).

‍Calling a smart contract means to access the internal code that is in a smart contract and the data included in it. This is a way of retrieving data from the contract. 

1. The Main Contract (MainContract)

This is the syntax of the MainContract calling its function:



contract MainContract {
     uint x; 
     // someone external calls main 
      function main() external {
          // inside main, we call the private store function
	store(10);
      }

      function store(uint _x) public {
x = _x;
      }
}

2. A Contract Inherited from the Main Contract (DerivedContract)

This is the syntax of a DerivedContract that is using a derived function from the MainContract:



contract DerivedContract is MainContract {
      function newFunction() external {
          // call a function derived from the MainContract
          store(15);
     }
}

3. A 3rd-Party Contract (OutsideContract)

This is the syntax of an OutsideContract that is is calling the main contract from an outside function.



contract OutsideContract {
      function functionOutside(address mainContractAddress) external {
            // here we send a message call to main contract 
            MainContract(mainContractAddress).store(15);
      }
}

How do function visibility modifiers and inheritance work together?

Inheritance is when the contents of one contract are copied into another contract (a “derived contract”) by using the “is” keyword, and the function modifier visibility works with a derived child contract according to the type of visibility a contract is assigned.



Contract ParentContract {
    function performTask1() public {
            //performs the first task
              }
}
 
Contract ChildContract is ParentContract {
        	function performTask2() public {
            //performs a second task
              }
}

The relationship between function visibility modifiers and inheritance is explained below:

  • If the modifier of the ParentContract function is public, a ChildContract can access it.
  • If the modifier of the ParentContract function is internal, the ChildContract can access it.
  • If the modifier of the ParentContract function is private, the inheriting ChildContract cannot access it. 
  • If the modifier of the ParentContract function is external, the inheriting ChildContract cannot access it.

How can function visibility modifiers help gas optimization?

Because the parameters for external function visibility modifier are not saved to memory, but read directly from calldata, smart contracts consume less gas. In contrast public functions use input parameters are saved to memory, which costs more gas to deploy a smart contract.

What are function visibility modifiers?

There are four different types of function visibility modifiers that range from most accessible to least accessible: public, external, internal, and private. Developers should modify functions with the correct visibility based on who they want to be able to see and call the function.

1. Public

A public function can be accessed by any of the three types of calling contracts: main contract, derived contract, and a third party contract. A function is public by default.



contract RandomContract {
      function functionPublic() public {
            //performs a task
              }
}

As shown in the code sample above, any contract in the codebase can access the “functionPublic” function because of the “public” visibility.  

2. External

An external function is a function that can only be called by a third party. With the external function visibility, a contract that can call the function must be independent of the main contract and can not be a derived contract.



contract RandomContract {
      function externalFunction() external {
            //performs a task
      }
}

Above is a random contract (RandomContract) that is performing some task. The code snippet below is an external contract that is calling the external function on the RandomContract.



interface IRandomContract {
	function externalFunction() external;
}
contract CallingContract {
        	function functionIsExternalCall(address addr) external {
	IRandomContract(addr).externalFunction();
          }
}

3. Internal

An internal function can be called by the main contract and any of its derived contracts. Internal functions are accessible from the main contract in which they were initially declared and by the contracts that extend from this main contract through inheritance. 

4. Private

A private function can only be called by the main contract in which it was specified. Private functions are used initially according to common practice, but if the scope is wider than this modifier type, any other plausible modifier should be used.

What is the default function visibility in Solidity?

If the visibility modifier of a function is not explicitly declared in the code, the function is set to public visibility by default. Leaving the function visibility blank and relying on the Solidity compiler to use the default visibility is not a recommended best practice; intentionally setting the function visibility makes code more legible and easier to understand during code reviews.

What is the difference between function and state variable visibility modifiers?

The difference between function visibility modifiers and state variable visibility modifiers is that state variables do not have the external visibility modifier option. 

State variables are variables whose values are permanently stored in contract storage, which holds data persistently between function calls. Like function visibility modifiers, state variables also have visibility modifiers which are Public, Private and Internal. 

State Variable Visibility Modifiers

State variables are declared in the contract section of the program. The variable data type is specified and after that, the visibility modifier is assigned to the variable. 



Contract TestingVariableStorage {
      uint public publicData; // Publicly visible variable 
        	string private privateData; // Privately visible variable
        	string internal internalData; // Internally visible variable
}

1. Public

A state variable with the public modifier can be accessed by any contract in the application. If a variable is declared as public, the data stored in it can be read by the main contract, a derived contract, or an external contract. 

2. Internal

State variables declared with the internal modifier can only be accessed within the contract in which they were defined and by a derived contract. A third party or another external contract cannot access the storage of the data defined as an internal variable. 

3. Private

If a state variable is private, only the main contract in which they were declared can call them. The private visibility modifier restricts access from other parties apart from the main contract so that the data it holds is protected. 

What is the default variable visibility in Solidity?

When the variable visibility is not defined, the default value is internal. It is best practice to declare a variable as private until the scope widens.

Build web3 with Alchemy

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get started for free