- 
  Notifications
 
You must be signed in to change notification settings  - Fork 3.3k
 
Functions in solidity explained (Notes that I made which made me understand them fully) #4957
-
State Variables: Variable that are declared outside the functions are known as state variables. They can be accessed anywhere inside the contract.
Local Variables: Variables that are declared inside the function are known as local variables. They can only be accessed/used inside a function. We can’t access/use them outside the function body.
Functions:
Functions is a set of code that we can use anytime we want to. For example, we can write a function that will change the value of integer or calculate something for us. Whenever we want to use a function, we "call" it in order to execute the code in the function.
Mechanism behind interaction with blockchain (VERY IMPORTANT)
Interaction with the blockchain happens in the form of function calls. In the previous lab whenever we click to see the value of variables in Remix, we are calling a function that interacts with the blockchain and reads the value of those two variables to us. There are two types of interaction with the blockchain:
- The first one is where we are just reading or accessing something from a blockchain. For example let’s say we want to read the value of integer variable from the blockchain. In this case, we are not changing anything on the blockchain, therefore this type of interaction is non-deploying since we are just reading. These function calls do not cost any gas fees.
 - The second type of interaction is where we are changing something on the blockchain. Any time we are changing anything on the blockchain, we have to deploy the change on the chain and as a result it costs us money. For example, let’s say we call a function that allows us to change the value of an integer variable in the chain. Since this function is changing something on blockchain (in this case a value of a variable), it’s a deployment call and thus cost gas to execute. Deploying a smart contract on a blockchain is another example of this because we are adding on something to blockchain and changing it.
 
Gas Fees: Money required to deploy or change something on the blockchain.
Pure: We use this keyword in a function if the function is not changing anything on the blockchain and is also not reading anything from the blockchain.
View: We use this keyword in a function if the function is not changing anything on a blockchain and is reading something from the blockchain. Basically, if the function is using state variables then we have to use view keyword (since we are reading the variables, we are not changing the value of anything).
Arguments and parameters in functions.: Arguments are the values that we pass in the function. Consider it the input that we pass into the function to get whatever output we are trying to receive from the function. While parameter is the variable name of that input.
When will you use view function?
- When you are not changing anything on the blockchain
 - When you are wanting to return a value stored on the blockchain (state variable)
For example, let’s say we have a smart contract where I declare a state variable "country". If I want to return the value of country then I would use a view function since I am not changing anything from the blockchain, I am just returning the value stored in the state variable (think about it as just reading from something store on the blockchain). 
When will you use a pure function?
- When you are not changing anything on the blockchain.
 - When you are just wanting to return a result that doesn’t require any state variable.
In most cases, a pure function is used to do basic math operations. Let’s say you have a smart contract where you need a function to add 2 to the value that’s passed as an argument. Since in this case, you are not using any state variable (refer to definition of state variable and local variables if you are confused) and are just using a local variable, you will use a pure function. Think of pure function as something that only uses the value that’s passed in the function and doesn’t change anything else in the contract. 
Comparison between view and pure
View:
- Does not change the blockchain state
 - Reads from the value stored on the blockchain
 - Requires state variable from the smart contract
 
Pure:
-Does not change state
- Does not read anything from blockchain
 - Don't use state variable from smart contract
 
I have prepared this flowchart that you can refer to:
image 
Beta Was this translation helpful? Give feedback.
All reactions
- 
 
👍 1