In solidity, the permission to call a function can be restricted to 4 different permission levels. These are called Access Modifiers and include Public, Private, Internal, and External. The main benefit of controlling access to a function is security. Here is an example with each modifier:
//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract accessModifiers{
function publicExample() public view returns(uint){
//anyone can call this function
return 1;
}
function privateExample() private view returns(uint){
return 2;
}
function callAPrivateFunction() public view returns(uint){
return privateExample();
}
function externalExample() public view returns(uint){
return 3;
}
function callAnExternalFunction() public view returns(uint){
return externalExample();
}
function internalExample() public view returns (uint){
return 4;
}
}
contract usingInternal is accessModifiers{
function callAnInternalFunction() public view returns(uint){
return internalExample();
}
}
A “Public” function can be called by anyone, including you, another developer, or even a smart contract.
A “Private” function can only be called by the smart contract that contains it.
An “External” function may be called by any user of the contract, or other contracts calling it, but not by the contract itself.
An “Internal” function can only be called by other functions in it’s smart contract, or other functions that are in smart contracts built upon itself. In short, any contract that inherits a function with the “internal” access modifier may use it.