Solidity Basics

A function looks like this:

pure means we don’t access any states in the contract. view means we don’t update the state but only view it.

It’s a convention to put _ before private functions or function arguments.

You also have to specify which type you return. Note that you shouldn’t forget memory or other data area (storage, calldata) where the type is stored when defining a parameter for reference types. The reference types include string, struct, and array. Updating their value in the function changes the original value where as updating value types only change their copies.

In Ethereum, there are accounts and each of them has its unique address. The function in a smart contract is never executed until someone calls it. We can use msg.sender to get that person’s address.

You can create a map in Solidity.

Pretty straightforward.

There’s a keyword equivalent to assert in Python.

Look at the LINE 1 where it uses require. It raises an error if the logic statement doesn’t meet the condition. This way, we can ensure the code runs only when the condition we want is met.

Inheritance is pretty straightforward and you can do it just to make codes cleaner.

  • public: any other functions can call it
  • private: only the functions inside the same contract can call it
  • external: only the functions outside the contract can call it
  • internal: only the functions inside the same contract and its inheritors can call it

We can use a code deployed on Ethereum by using an interface if we know its address on the network.

Let’s say that there is a contract like this:

In our code, we should declare an interface that can interact with the contract above. By doing so, we let our contract know what functions we used and how they look like.

You just copy-paste the function you’d like to use.

To actually use the interface,

A function in Solidity has an interesting property: it can return multiple values.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store