Market Cap: $2.8389T -0.70%
Volume(24h): $167.3711B 6.46%
Fear & Greed Index:

28 - Fear

  • Market Cap: $2.8389T -0.70%
  • Volume(24h): $167.3711B 6.46%
  • Fear & Greed Index:
  • Market Cap: $2.8389T -0.70%
Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos
Top Cryptospedia

Select Language

Select Language

Select Currency

Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos

What is the difference between a public, private, internal, and external function in Solidity?

Solidity's function visibility specifiers—public, private, internal, and external—control access to functions, impacting security, inheritance, and gas costs in smart contracts.

Nov 20, 2025 at 10:39 pm

Understanding Function Visibility in Solidity

Solidity, the primary programming language for Ethereum smart contracts, enforces strict access control through function visibility specifiers. These specifiers determine who can call a function—whether it’s from within the contract, derived contracts, or externally. The four types—public, private, internal, and external—define the scope of accessibility and play a crucial role in securing contract logic.

Public Functions

1. Public functions are accessible from anywhere—inside the contract, derived contracts, and externally via transactions or calls.

2. When a function is marked as public, Solidity automatically generates a getter function if it’s a state variable or allows direct invocation through the contract interface.

3. These functions become part of the contract’s ABI (Application Binary Interface), making them callable by wallets, dApps, and other smart contracts.

4. Gas costs may be higher for public functions due to external call overhead and data encoding requirements.

5. A common use case includes user-facing functions like token transfers or balance checks in ERC-20 contracts.

Private Functions

1. Private functions can only be called from within the same contract and are completely inaccessible to derived contracts or external entities.

2. They are ideal for encapsulating sensitive logic that should not be exposed or overridden.

3. Since they are not part of the external interface, private functions do not appear in the ABI.

4. Developers often use them for internal validation, hashing, or cleanup routines that support public or internal operations.

5. Despite being private, their code is still visible on-chain; privacy refers only to execution access, not source concealment.

Internal Functions

1. Internal functions are accessible within the defining contract and any contract that inherits from it.

2. They cannot be called directly by external accounts or unrelated contracts, even through address-based interactions.

3. Inheritance hierarchies rely heavily on internal functions to share reusable logic without exposing it publicly.

4. Like private functions, they do not contribute to the contract’s external ABI.

5. An example includes utility functions in OpenZeppelin’s SafeMath library or modifiers that enforce preconditions across multiple methods.

External Functions

1. External functions can only be called from outside the contract—either by another contract or an EOA (Externally Owned Account).

2. Even the contract itself must use this.functionName() syntax to invoke its own external functions, which incurs a message call overhead.

3. This visibility is useful when you want to ensure a function is never executed internally, promoting modularity and reducing gas misuse.

4. External functions are included in the ABI and can be invoked via transaction or contract-to-contract calls.

5. They are commonly used for large data inputs, as calldata (used by external functions) avoids memory copying costs associated with internal calls.

Frequently Asked Questions

Can a private function be overridden in a derived contract?No, private functions cannot be accessed or overridden by derived contracts. Only internal and public functions can be overridden using the virtual and override keywords.

What happens if I don’t specify a visibility modifier?If no visibility is specified, Solidity defaults to public for functions. For state variables, the default is internal. Relying on defaults is discouraged for security and clarity reasons.

Is there a performance difference between internal and external function calls?Yes. Internal calls execute within the same contract context and use minimal gas. External calls, even when self-referenced, require a message call, increasing gas consumption due to stack isolation and ABI encoding.

Can an external function modify state variables?Yes, external functions can modify state variables unless restricted by other modifiers like view or pure. Their ability to alter storage depends on logic, not visibility.

Disclaimer:info@kdj.com

The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!

If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.

Related knowledge

See all articles

User not found or password invalid

Your input is correct