Difference between internal and external functions in Solidity

Internal functions

InternalIt is the type of scope or access of variables and functions in a contract. functions are the type of functions that can only be called internally within the contract or by the contracts inherited from the current one.

Note: We can also assign an internal quantifier to the variables of the contract.

Code example

Let’s look at the code below:

pragma solidity ^0.5.0;
contract ContractBase
{
//private state variable
uint private data;
//constructor
constructor() public
{
data = 80;
}
//internal function
function multiply(uint num1, uint num2) internal pure returns (uint)
{
return num1 * num2;
}
}
//inherited Contract
contract ContractDerived is ContractBase
{
uint private result;
constructor() public {
result=0;
}
function getResult() public returns(uint)
{
result = multiply(55, 7);
return result;
}
}

Code explanation

Here is a line-by-line explanation of the code above:

  • Line 3: We create a contract ContractBase.

  • Lines 15 – 18: We define the internal function multiply in the ContractBase contract to multiply two numbers and return its result.

  • Line 22: We create a contract ContractDerived inherited from ContractBase to test the internal function.

  • Line 33: We call the internal function multiply of the ContractBase contract in the derived contract ContractDerived.

External functions

ExternalIt is the type of scope or access of functions in a contract. functions are the type of functions that are part of the contract but can only be used externally and called outside the contract by the other contracts.

Note:

  • We can explicitly call the external function using thisIt is the pointer pointing to the current contract. keyword within the contract.
  • We can assign an external quantifier only to functions but not the variable of the contract.

Code example

Let’s look at the code below:

pragma solidity ^0.5.0;
contract ContractOne
{
//private state variable
uint private data;
//constructor
constructor() public
{
data = 70;
}
//external function
function multiply(uint num1, uint num2) external pure returns (uint)
{
return num1 * num2;
}
}
//external Contract
contract ExternalContract
{
uint private result;
ContractOne private obj;
constructor() public {
obj = new ContractOne();
}
function getResult() public returns(uint)
{
result = obj.multiply(70, 86);
return result;
}
}

Code explanation

Here is a line-by-line explanation of the code above:

  • Line 3: We create a contract ContractOne.

  • Lines 15 – 18: We define the external function multiply in the ContractOne contract to multiply two numbers and return its result.

  • Line 22: We create an external contract ExternalContract to test the external function.

  • Lines 25: We make a private state variable obj of type ContractOne to use its external function multiply in the external contract ExternalContract.

  • Line 33: We call the external function multiply against the obj in ExternalContract.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved