Quick Guide To State Variables

In this article, we will explore state variables and different types of Solidity variables and their storage mechanisms, along with best practices for declaring and initializing variables in smart contracts.

There are three types of variables supported by Solidity:

 VariablesOverview
State variables(also called contract variables) are variables with values that remain in the contract permanently.
Local VariablesVariables whose values remain till the function is executed.
Special variablesexist in the global namespace and are used to get information about the blockchain.

As a statically typed language, Solidity requires a state or local variable type during declaration.

There is always a default value for each declared variable based on the type of the variable.

Please note Undefined and null do not exist here.


State Variable

State variables are declared outside of any function and have a permanent storage location in the contract.

They can be accessed and modified by any function within the contract.

State variables are typically used to store information that needs to persist between function calls or that needs to be shared between functions.

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest {uint state_variable;constructor() public{ state_variable=24*3; }function get_stateVariable() public view returns(uint){ return state_variable; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Variables example output1

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest {uint state_mrx_1; uint state_ample_2;constructor() public{ state_mrx_1=14; state_ample_2=6; }function get_stateVariable() public view returns(uint){ return state_mrx_1/state_ample_2; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Variables example output

2 is returned because the output is returned as an unsigned integer as well.


Solidity Local Variable

Local variables, on the other hand, are declared within a function and exist only for the duration of that function’s execution.

They are not stored permanently in the contract and cannot be accessed by other functions.

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest {uint state_variable;constructor() public{ state_variable=24*3; }function get_stateVariable() public view returns(uint){ return state_variable; }function get_LocalVariable() public view returns(uint){uint mrx=20; uint ample=2; uint divisor=mrx/ample; return divisor;} }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest {uint state_variable;constructor() public{ state_variable=5+5; }function get_stateVariable() public view returns(uint){ return state_variable; }function get_Product_LocalVariable() public view returns(uint){uint mrx=6; uint ample=3; uint product=mrx*ample; return product;} }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract SolidityTest {uint state_mrx_1; uint state_ample_2;constructor() public{ state_mrx_1=20; state_ample_2=6; }function get_Product_stateVariable() public view returns(uint){ return state_mrx_1*state_ample_2; } function get_Sum_localvariables() public view returns(uint){ uint local_mrx=4; uint local_ample=10; uint sum= local_mrx+local_ample;return sum; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
The Solidity First Application chapter provides instructions that will help you to execute the above program.

Solidity Global Variables

There are special variables in the global workspace that provide information about blockchains and transactions.

NamesReturns
blockhash(uint blockNumber) returns (bytes32)The hash of the given block – only works for the 256 most recent blocks, excluding the current, blocks
block.coinbase (address payable)Provides the address of the current block miner
block.difficulty (uint)Gives the current difficulty of the block
block.gaslimit (uint)Shows the current block gas limit
block.number (uint)Display current block number
block.timestamp (uint)gives Current block timestamp as seconds since unix epoch
gasleft() returns (uint256)Shows the remaining gas
msg.data (bytes calldata)Displays Complete calldata
msg.sender (address payable)Displays the  Sender of the message (current caller)
msg.sig (bytes4)Shows the First four bytes of the calldata (function identifier)
msg.value (uint)Gives number of wei suffix(1 x 10^ 12) sent with the message
now (uint)Displays Current block timestamp
tx.gasprice (uint)Shows gas price of the transaction
tx.origin (address payable)provides the details of the sender  transaction

Solidity Variabler Name

You should keep the following rules in mind when naming your Solidity variables.

It is not recommended to use Solidity reserved keywords as variable names.

The next section discusses these keywords. Variable names like break or boolean are not allowed.

The names of solidity variables should not begin with a number (0-9). Letters or underscores must be used as the first character.

A variable name like 123test is invalid, whereas _123test is valid.

Case is taken into account when naming Solidity variables.

Similarly, a variable initialized by Name and a variable initialized by name are different.

 

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 *