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:

TypesOverview
PublicA public state variable can be accessed both internally and through messages. Automatic getters are generated for public state variables.
InternalState variables can only be accessed internally from the current contract or from contracts derived from it.
PrivatePrivate 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 

pragma solidity ^0.8.0; contract SolidityTest{uint public mrx=30; uint internal ample=20;function get_Value_Local_Variable_Contract1() public returns(uint){ // Modifying value of the public State Variable mrx = 4; return mrx; } }contract SolidityTest1{ SolidityTest obj=new SolidityTest(); function public_variables_external_value() public view returns(uint){ return obj.mrx(); // Here we make an external access to the public state variable } }contract SolidityTest2 is SolidityTest{function internal_variables_extended_contract() public returns(uint){ ample=11; return ample; // Here we make a call to the internall variable value }function local_variable_get_value() public pure returns(uint){uint mrx_1=7; uint ample_1=6;uint mrx_multiply_ample=mrx_1*ample_1;return mrx_multiply_ample; // Returns product of the local variables under the function } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

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:

ConceptsExplanation
State variablesThe 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 variablesThe get_Value_Local_Variable_Contract1() and internal_variables_extended_contract() functions modify the value of the state variables in their respective contracts.
External contract accessThe SolidityTest1 contract demonstrates how to create an instance of a contract and access its public state variable from a separate contract.
Local variablesThe 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

pragma solidity ^0.8.0; contract SolidityTest {uint public mrx=10; uint internal ample=6; uint private mr=5;function get_Public_value() public view returns(uint){// The line below prints the public variable mrx value inside the contract where it was initialized return mrx;}function get_Internal_value() public view returns(uint){ // Here, we print the value of the internal Variable ample in its parent contract return ample; }function get_Private_value() public view returns(uint){ // Now we print the value of the private variable mr which can not be used except the contract it was created in return mr; } }contract SolidityTest1{SolidityTest my_object=new SolidityTest();function get_public_variable_value_outside() public view returns(uint){ // The value of public variable is accessible in any contract by creating the object of its parent classreturn my_object.mrx(); } }contract SolidityTest2 is SolidityTest{ // The instance of SolidityTest3 is created with its parent contract named SolidityTestfunction get_Internal_Variable_Value() public view returns(uint){return ample; // Through } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *