1

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.

1 Answer 1

0
  • 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.

  • Variables that are declared as a state are always stored in the storage.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.