Variable Scope In Solidity
In this article, we’ll take a closer look at the different Solidity variable scopes with examples and how they can be used to create more robust and secure smart contracts.
Variable scope refers to the parts of the code where a variable can be accessed.
Solidity Variable Scope
In Solidity, variables can have either global or local scope. Global variables are declared outside of any function and can be accessed from any part of the code.
Local variables, on the other hand, are declared inside a function and can only be accessed within that function.
Solidity variable scope local is limited to the function in which it is defined, but a state variable’s scope can be divided into three types:
Types | Overview |
Public | A public state variable can be accessed both internally and through messages. Automatic getters are generated for public state variables. |
Internal | State variables can only be accessed internally from the current contract or from contracts derived from it. |
Private | Private state variables can only be accessed from within the current contract in which they are defined, not in derived contracts. |
The following code demonstrates the working of public and internal state variables:
Solidity Variable Scope Example: 1 
Example 1 Explanation
Above example is consists of three different contracts, each demonstrating different aspects of Solidity:
The SolidityTest contract defines a public state variable mrx and an internal state variable ample.
It also defines a function get_Value_Local_Variable_Contract1() which modifies the value of the mrx variable and returns it.
The SolidityTest1 contract creates an instance of SolidityTest using the new keyword and defines a function public_variables_external_value() which returns the value of the public state variable mrx of the SolidityTest contract.
The SolidityTest2 contract inherits from SolidityTest and defines a function internal_variables_extended_contract() which modifies the value of the internal state variable ample and returns it. It also defines a function local_variable_get_value() which defines local variables mrx_1 and ample_1, multiplies them and returns the product.
In summary, this code demonstrates the following Solidity concepts:
Concepts | Explanation |
State variables | The contracts demonstrate the use of public and internal state variables in Solidity. Public variables can be accessed both internally and externally, whereas internal variables can only be accessed internally within the contract and any contracts that inherit from it. |
Modifying state variables | The get_Value_Local_Variable_Contract1() and internal_variables_extended_contract() functions modify the value of the state variables in their respective contracts. |
External contract access | The SolidityTest1 contract demonstrates how to create an instance of a contract and access its public state variable from a separate contract. |
Local variables | The local_variable_get_value() function demonstrates the use of local variables in Solidity, which are used to store data within a function and are destroyed when the function returns. |
Solidity Variable Scope Example: 2 
Example
Example 2 Explanation
In above example SolidityTest contract defines a public state variable mrx, an internal state variable ample, and a private state variable mr. It also defines three functions: get_Public_value(), get_Internal_value(), and get_Private_value(), which return the values of the public, internal, and private state variables respectively.
The SolidityTest1 contract creates an instance of SolidityTest using the new keyword and defines a function get_public_variable_value_outside() which returns the value of the public state variable mrx of the SolidityTest contract. This demonstrates how to access public state variables of one contract from another contract.
The SolidityTest2 contract inherits from SolidityTest and defines a function get_Internal_Variable_Value() which returns the value of the internal state variable ample.
This demonstrates how to access internal state variables of the parent contract from the child contract.