Learn Solidity : A Cheat-Sheet

1 : Intro , Value-Types , Function Visibility

Learn Solidity  : A Cheat-Sheet

Intro

You have already herd a lot about what is solidity. If you haven't , then understand is a a base language to write Smart Contracts . It is a statically typed language . It will be a little bit of difficult for a total new beginner but just remember one thing.

You just have to explicitly write the TYPE of variable that you are going to use .

🤔What does that mean??

Eg. You just have to write const a = true as bool a = true.

I am not going to dig deep about What is Solidity? , Why is Solidity? , How is Solidity? ; Every second guy has already done that , So if you have no idea what solidity is then I will not recommend you to read further.

Now Let's Start understanding the Smart Contract 😎

// SPDX-License-Identifier : MIT  

pragma solidity ^0.8.0;

contract IAmSmart {

// Every variable that we statically type , and that are not inside any function;
// we call them as STATE VARIABLE
bool public stateVariable = true ;  

// Other types of State Variable
 // uint256 fav = 6 ; // uint8 to uint 256
 // string fav = "five";
 // int256 fav = -7; //positive + negative number also it is from int8 to int256
 // address myAddress = 0x6Fbe3f76227180d53b6F68f7383Fe3CC1ab610FE ; 
 // bytes32 fav = "cat";

// To know the maximum & minimum value that is supported by the int
int public maxInt = type(int).max

int public minInt = type(int).min

//more about this below
function subtract (uint a  , uint b)external pure returns(uint){      
return a - b ;
}
// A pure type because it is totally depend on the arguments that we will be passing from outside.
}

Breaking it up ☢

// SPDX-License-Identifier : MIT is some traditional line that you have to include so that you Compiler doesn't get mad at you.

pragma solidity ^0.8.0; is the way that we are telling the compiler that we are using Solidity version that is UP ^ from the 0.8.0 version .

After these two ritual lines have been done than we had started writing or smart contract . You have to write the smart contract by just using the contract followed by the ContractName . After that almost 90% of the things are going to happen inside that curly braces {...}

Types of state Variable 🦓🐴🦄

If you are into coding you may have already know what is Boolean and string . (true or false) & "You know who I am?😍" pretty much self-explanatory.

The second one is call unsigned integer or uint ; All the positive numbers fall in this category . Uint have also some variations like uint8 , uint16 ...etc , uint is the alias (second name) of uint256

uint8 can have value between 0 to 2**8 - 1

uint16 can have value between 0 to 2**16 - 1 ....etc

Int or integers are similar to uint but the major difference is that all the positive numbers ALONG with Negative numbers falls in the category of int . They have also same variations like int8 , int16 ...etc . int is also the alias of int256

int8 can have value between -2**8 to 2**8 - 1

int16 can have value between -2**16 to 2**16 - 1 ....etc

With all the above types Solidity also have some other types too.

Address could basically refers to the address of the owner , contract , other contract ...etc . An address is always pre-fixed with 0x as it is represented in hexadecimal format (base 16 notation) we will discuss later what actually does that mean(or you can google it if you are curious) .

Byte are treated as array is Solidity code, it can have a length of zero and you can do things like append a byte to the end. You will get to know more about it when you will bw working with the cryptographic hash function keccak-256

All the above types that i have explained are the most used data types and you will get to know more in future.

Functions

Like in Javascript , Functions of solidity are similar to what we write in JS . There is a slight change in syntax , if we are returning anything from that function we have to explicitly write what we are returning , like if want our function to return uint we have to explicitly write like this::

function add(uint a , uint b)external view returns(uint){
return a + b ;
}

There are four types of function visibility in Solidity:

external :

The functions which is been only called by the third-party means outside the contract then we say that its a external function . (i.e : we cant call a external function into another function anyexternalfunction()... ) . It is also GAS efficient.

internal :

A internal functions can be called by the main contract and the contract instance and derivatives of contract.

private :

A private function is one that can only be called by the main contract itself

public:

A public function can be called from all potential parties . It also creates security vulnerabilities , So only make a function public when you really want it to be .

Apart from all that we have 3 other type addition to the functions

  1. view : It is used to read the state variables and it does not modify the state variable , when we calling it.

  2. pure : When a Function is purely depends on the arguments for their output we name them as a pure function. It does not reads the State Variable and also does not modify it .

  3. payable : When ever we want any function to receive or make any payment we name them as payable .

So , That all for this part , We will cover some important topics as well in the upcomming parts , till then make sure you follow this blog and also share it with the friends who are learning Solidity😊.

#WAGMI