SOLIDITY - 101 (PART 1)
Tanisk Annpurna
Posted on July 17, 2022
You can read my last article about COMPILING SMART CONTRACT, here ๐
Colmpiling Smart Contract
Let's Start
๐ VARIABLES
- Variables are data items that is defined to store different values for the runtime of the program.
- We have different type of values to store in program like it can be an Integer, String, Characters, boolean etc.
- 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.
Unsigned Integers
- Numbers can only positive but not negative or decimals
uint -> This is to tell solidity that we will store only positive integer in this variable.
Boolean
- True or False
bool -> This is to tell solidity that we will store true or false in this variable.
String
- A combination characters. It can be a word, sentence etc.
string-> This is to tell solidity that we will store combination of characters in this variable.
Address
- Its a special type of data that is used in smart contract. As EVM has an hexadecimal unique value for every accounts or smart contract. We call them address as EVM will interact with the account/contract using the address.
address-> This is to tell solidity that we will store Address in this variable.
๐ ACCESS IDENTIFIER
- Access Identifier means that we can decide who can access the variables/function or anything in the smart contract.
- For Security purposes, We need to restrict the access. We do this using access Identifier.
PUBLIC -> This means that it can be accessed from anywhere, anyone can access it. No Restrictions.
PRIVATE -> This means that it can only be accessed from the contract in which its present. No contract/account from outside the contract can access it. Although they can see it but can not be accessed.
INTERNAL -> This means that no outside contract/accounts can access it. It can be accessed from within and contract's children i.e. Contracts inherited from this contract can access it. This is the default identifier i.e. If no access identifier is provided, EVM assumes this access identifier.
EXTERNAL -> This means that only external contracts/accounts can access it. if we need to use it internally then we have to use "this" keyword.
๐ DEFINING A VARIABLE
- We define variable in solidity as this pattern.
<DATA_TYPE> <ACCESS IDENTIFIER> <VARIABLE NAME>
int private fav_num = -8;
uint public fav_num = 8;
bool internal is_fav_num = true;
string external word = "Hello";
address private contractAddress = 0x70997970C.......;
๐ Remember, We can also omit the access identifier and EVM by default will take internal as access identifier.
๐ Remember, Always try to use Variable Name as self-explanatory.
๐ Remember, We use " " for defining strings and ; to end any syntax.
๐ 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.
๐ DEFINING FUNCTIONS
- We define function in solidity as this pattern.
function <FUNCTION_NAME> <ACCESS IDENTIFIER> <SPECIAL ACCESS IDENTIFIER(OPTIONAL)> returns(<DATA_TYPE>){}
function func_name public pure returns(uint){}
function func_name2 private view returns(string){}
๐ Remember, We can also omit the special access identifier.
๐ Remember, Always try to use Function Name as self-explanatory.
๐ 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];
}
That's all.
In the next article, we will look why we need special access identifiers, how we can add on these variables and use it more space effeciently.
Hello, I am Tanisk Annpurna
I post about ๐web3, Blockchain, Ethereum ๐ฆSmart Contract, Solidity ๐JavaScript, ReactJS, NodeJS Follow and like for more such posts. !!โ๏ธ!!
Posted on July 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.