2 min read

Functions Internal and External

Introduction

Functions in Solidity 0.8.16 can be declared with different visibility specifiers. These specifiers determine how functions can be accessed within a smart contract or from external contracts. In this blog post, we will explain the difference between internal and external functions in Solidity 0.8.16 and provide examples of how to use them.

Internal Functions in Solidity 0.8.16

Internal functions in Solidity 0.8.16 are declared with the internal keyword. Internal functions can be accessed within the same contract and any derived contracts, but cannot be called from external contracts. Here is an example of an internal function in Solidity:

pragma solidity 0.8.16;

contract Example {
    uint private value;

    function getValue() internal view returns (uint) {
        return value;
    }
}

In this example, we have defined a contract called Example with an internal function called getValue(). The function returns the value of a private variable called value. Because the function is declared as internal, it can only be called from within the same contract or any derived contracts.

External Functions in Solidity 0.8.16

External functions in Solidity 0.8.16 are declared with the external keyword. External functions can be called from external contracts, but cannot be accessed within the same contract or any derived contracts. Here is an example of an external function in Solidity:

pragma solidity 0.8.16;

contract Example {
    function doSomething() external {
        // Function logic goes here
    }
}

In this example, we have defined a contract called Example with an external function called doSomething(). The function can be called from external contracts, but cannot be accessed within the same contract or any derived contracts.

Benefits of Using Internal and External Functions

There are several benefits to using internal and external functions in Solidity:

  1. Code Reusability: Internal functions can be used within the same contract and any derived contracts, making it easy to reuse code across different parts of the contract hierarchy. External functions can be called from external contracts, making it easy to reuse code across different contracts.
  2. Security: By using internal functions, you can restrict access to certain functions within a contract, making it more secure and less vulnerable to attacks. By using external functions, you can expose certain functions to external contracts, while keeping other functions private.
  3. Gas Optimization: By using internal functions instead of external functions, you can save on gas costs, since internal function calls do not require a contract call.

Conclusion

In this blog post, we explained the difference between internal and external functions in Solidity 0.8.16 and provided examples of how to use them. By using internal and external functions in your Solidity code, you can make your contracts more reusable, secure, and efficient.

Feedback

Have feedback? Found something that needs to be updated, accurate, or explained?

Join our discord and share what can be improved.

Any and all feedback is welcomed.