Solidity storage types are a bit confusing for beginners. If you’re reading this post, I can bet you’re confused between Memory and Calldata. Don’t worry, I was in that situation, and in this post, you will understand the difference b/w Storage vs Memory vs Calldata and when to use what.
Storage
All the variables and arrays declared outside of the function are state variables and stored in Storage by default. Storage means data is stored directly on Blockchain. You can also declare a variable of storage type inside the function by using the keyword storage. In the below example, variable twitterAccount is declared outside of the function, and by default, its storage type is Storage so this will be permanently stored on the Blockchain. I love storage type because it is very simple to explain and use in the smart contract but storage type is very expensive so be mindful of what exactly you want to store on Blockchain because it costs you real money.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CodiesAlert {
string twitterAccount = "Anni_Maan";
function displayAccount() public view returns(string memory) {
string storage Newvar;
return twitterAccount;
}
}
Memory
The second storage type is a memory which is temporary storage. You can declare memory type variables only inside the function or to receive the function parameters. You can also modify _accountName variable inside your function. I know you would think of course I can modify a function parameter inside that function isn’t it obvious. Well, hold that thought and you would know why I explicitly highlighted this point. Once function execution is completed these variables are cleared for memory.
You can think of memory as your laptop RAM, you will have access to unsaved data as long as the laptop is running but once you shut down your laptop your data is cleared from RAM if it is not saved.
You use memory variables for temporary calculations or temporary storage.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CodiesAlert {
string twitterAccount = "Anni_Maan";
function displayAccount(string memory _accountName) public pure returns(string memory) {
return _accountName;
}
}
Calldata
The third storage type is calldata which is also a temporary storage just like memory. You might be wondering memory storage is already there then why do we need another temporary storage.
Calldata is indeed temporary storage but it is a little different from memory. The main difference is calldata storage is non-modifiable storage which means once function parameters are passed you cannot modify them inside the function.
The second big difference is it is cheaper than memory and is mostly used with external function types. Mostly you would see calldata used with external functions.
If you google about external functions in solidity you will find people saying external functions cannot be called inside the contract but not entirely true. You can still call a function that is declared with an external keyword inside your contract by using this keyword.
You can see in the below example, I have called the result function inside the contract with this keyword.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CodiesAlert {
string name = "Anni";
// Access externally Marked function inside the contract
function accessExternal() public view returns(string memory) {
// this keyword is used to access externally marked function inside the contract
return this.result(name);
}
// Below function can be access externally as well as internally
function result(string calldata _a) external pure returns(string calldata) {
return _a;
}
}