DEV Community

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

Posted on

SOLIDITY - 101 (PART 1)

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

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

Boolean

  • True or False
bool -> This is to tell solidity that we will store true or false in this variable.
Enter fullscreen mode Exit fullscreen mode

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

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

๐Ÿ’œ 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.
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’œ 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){}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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];
}

Enter fullscreen mode Exit fullscreen mode

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. !!โœŒ๏ธ!!

Top comments (0)