DEV Community

Cover image for SOLIDITY-101 (PART 2)
Tanisk Annpurna
Tanisk Annpurna

Posted on

SOLIDITY-101 (PART 2)

You can read about basics of solidity in my last article here πŸ‘‡

Solidity-101 (part1)

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.
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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.
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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.......;

Enter fullscreen mode Exit fullscreen mode

πŸ’œ 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.

Enter fullscreen mode Exit fullscreen mode

πŸ’œ 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];
}

Enter fullscreen mode Exit fullscreen mode

πŸ’œ 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)