Function Visibility Modifier vs State Variable Visibility Modifier
The main difference between the function and state variable visibility modifier is that there are four function visibility specifiers whereas three state variable visibility specifiers. The function visibility specifiers are public, private, internal, and external but state variable visibility specifiers are public, private, and internal only. If the state variable visibility specifier is public then it can be accessed by all the contracts, if private then the contract in which the state variable is declared can access it and if internal then the contract declared it and its child contract can access it.
Visibility Specifier | Function | State variable |
---|---|---|
Public | All contracts can call the function with public visibility specifier. | The State variable declared with the public visibility Specifier can be accessed by all the contracts. |
Private | Only the contract containing the function can call the function with visibility Specifier private. | The State variables declared with private visibility Specifier can only be used by the same contract in which it is declared. |
Internal | The contract containing the function and its child contracts can call the function with internal visibility Specifier. | The State variable declared with internal visibility Specifier can be used by the same contract or its child contracts. |
External | The external contracts can call the function with external visibility Specifier. | The state variable does not have external visibility Specifier. |
Default visibility Specifier | The default visibility Specifier for functions is public in Solidity. | The default visibility Specifier for the State variable is internal in Solidity. |
Function Visibility Specifier in Solidity
Function in Solidity is a set of code that performs a specific task. The function provides reusability of code in smart contracts. In Solidity, functions are defined using the function keyword. The syntax of the function definition in Solidity is:
function function_name (parameter list) visibilty_specifier modifier returns (return_type)
{
Block of Code
}
Function visibility in Solidity defines the visibility of the function to other functions within the contract, or in other contracts. Function visibility helps to identify where in the contract or in other contracts, functions can be used. It is specified by the developer of the smart contract. The default variable visibility in Solidity is internal. The default function visibility in Solidity is public.