You can read about basics of solidity in my last article here π
Let's Start
π VARIABLES
- We have different type of values to store in program like it can be an Integer, String, Characters, boolean etc.
- There are variables for integer or unsigned Integer, we can add power of 2 after variables data type and it will restrict it to certain value.
- Variables in solidity are :
Integers
- Numbers can be negative as well as positive but not decimals
int -> This is to tell solidity that we will store integer in this variable.
int8 -> This will store values from -2^8-1 to 2^8-1 i.e. -255 to 255.
int16 -> This will store values from -2^16-1 to 2^16-1 i.e. -65536 to 65536.
.
.
.
int256 -> This is the maximum integer value solidity store i.e. -2^256-1 to 2^256-1.
π Remember, writing just int or int256 is same. By default int is taken as int256.
Unsigned Integers
- For Numbers that will be only positive but not negative or decimals
uint -> This is to tell solidity that we will store unsigned integer in this variable.
uint8 -> This will store values from 0 to 2^8-1 i.e. 0 to 255.
uint16 -> This will store values from 0 to 2^16-1 i.e. 0 to 65536.
.
.
.
uint256 -> This is the maximum integer value solidity store i.e. 0 to 2^256-1.
π Remember, writing just uint or uint256 is same. By default int is taken as uint256.
π This is only limited to integer and unsigned Integer.
π DEFINING A VARIABLE
- We define variable in solidity as this pattern.
<DATA_TYPE> <ACCESS IDENTIFIER> <VARIABLE NAME>
int8 private fav_num = -8;
uint16 public fav_num = 8;
bool internal is_fav_num = true;
string external word = "Hello";
address private contractAddress = 0x70997970C.......;
π SPECIAL ACCESS IDENTIFIER FOR FUNCTIONS
- These access identifier is just to tell EVM how the function will interact with the storage.
PURE -> This means that it will not even access the storage.
VIEW-> This means that it will only access the storage but wont change anything.
π EXAMPLE
address private i_owner;
uint256 public minimumUsd;
function getOwner() public view returns (address) {
return i_owner;
}
function getAddressToAmountFunded(address funder) public view returns (uint256) {
return s_addressToAmountFunded[funder];
}
π Summary
- This is done because we want to eliminate unwanted storage usage.
- So, if we know that values will be small then we can use uint8, uint16...etc.
- This is done to keep our gas prices in control.
- Depending on different task, EVM requires amount of gas to be paid. So to keep it in control we use these.
π Remember using PURE or VIEW -> SPECIAL ACCESS IDENTIFIER, its because these denotes that they tells EVM that they have very limited functionality and gas charge will be taken as per.
That's all.
In the next article, we will look into different Storage, Data Structures that we basically use in writing SMART CONTRACT.
Hello, I am Tanisk Annpurna
I post about πweb3, Blockchain, Ethereum π¦Smart Contract, Solidity πJavaScript, ReactJS, NodeJS Follow and like for more such posts. !!βοΈ!!
Top comments (0)