Functions in solidity explained (Notes that I made which made me understand them fully) #4957
369-shar-block
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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:
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?
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?
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:
Pure:
-Does not change state
I have prepared this flowchart that you can refer to:
Beta Was this translation helpful? Give feedback.
All reactions