Calldata in solidity

Favorite_blockchain_lady
2 min readOct 21, 2023

Ever wondered what calldata is in smart contracts? I was going through solidity documentation today and came across calldata. I remembered how confusing it was to understand what calldata data was, so I decided to put it out here for everyone who wants to have a good understanding of what it is in solidity.

According to solidity documentation, There are three data locations: memory, storage and calldata. Calldata is a non-modifiable, non-persistent area where function arguments are stored and behave mostly like memory. I will be giving two scenarios for better understanding. Calldata is the location where external values from outside a function into a function are stored. It is a nonmodifiable and non-persistent data location.

Scenario 1

Think of calldata in Solidity as a one-way street where information comes into a function from the outside world.

  • External Values: This is like messages or packages sent to the function. It’s where data from outside the smart contract is kept temporarily.
  • Non-Modifiable: It’s like a postbox; you can take out the letters, read them, but you can’t change what’s inside. In calldata, the data is read-only. You can't edit it.
  • Non-Persistent: This data doesn’t stick around; it’s used just for a moment during the function’s operation, then it’s gone. It’s like reading a letter and then throwing it away.
  • Required Keyword: Just like a postbox has a label saying “post,” calldata needs a special keyword to work. This keyword is used to define variables that are stored in calldata.

So, calldata is like a special place where the function can look at data from outside, but it can't change it, and this data is only temporary while the function is running.

Second Scenario

Let’s imagine calldata is like a read-only book that contains information sent to a smart contract. Imagine you're a librarian at a magical library.

  • Calldata is Read-Only: People can look at the books in your library, but they can’t write or change anything. That’s like calldata; it can't be modified.
  • Information in calldata: calldata contains data (like numbers, text, and instructions) that people send to your contract when they interact with it.
  • No Writing: Just like you don’t let people draw or write in your library books, calldata is safe from any changes. It's used for reading and checking, not for updating or modifying things in your contract.

calldata in Solidity is a place where your smart contract can look at the information people send, but it can't be changed, just like a librarian looking at books in a library but not allowing anyone to write in them.

--

--