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:
|State variables||(also called contract variables) are variables with values that remain in the contract permanently.|
|Local Variables||Variables whose values remain till the function is executed.|
|Special variables||exist 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.
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.
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.
Solidity Global Variables
There are special variables in the global workspace that provide information about blockchains and transactions.
|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.
Case is taken into account when naming Solidity variables.
Similarly, a variable initialized by Name and a variable initialized by name are different.