is there any reason why we should not store strings as storage in a function? I do not get the purpose of using it.
If anyone can explain it's greatly appreciated.
Variables declared as function parameters are either stored at a call data or memory data location.
Variables declared inside functions, are stored in a memory data location.
Every operation in a smart contract costs you money. So you have to be careful when you design a smart contract. Your variables that set in a storage will cost you no matter what, but variables inside functions will cost you when your functions are being called. It is kinda paid as you go.
When creating state variables
in any application, you have to be very careful not to modify them. Because if you modify them in one part of your code, another part of your code may not give you the desired result, and you will create a lot of bugs.
Imagine, you identified your function as public
, and variables and arguments are stored in storage. So anyone who calls your function will modify your state variables. To keep the state variables safe, inside functions we copy them into memory and use this copied value.