🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Solidity Smart Contracts Explained and Illustrated

Reading time 5 min
Published Jan 2, 2016
Updated Sep 30, 2019

In this Solidity smart contracts tutorial, we will explain in detail what a smart contract is, what advantages it has, and how it should be written.

Even though nowadays they are associated with blockchain, smart contracts actually are a much older concept. The first to describe them in the mid-nineties was Nick Szabo - a scientist with a background in computer technologies and cryptography. By combining the two, he came up with a way to ensure both speed and safety.

If you want to have a go at writing your own smart contract while also having fun, try our free interactive course.

Solidity Smart Contracts: Main Tips

  • Using smart contracts eliminates the need for a third party when executing an agreement.
  • All Solidity smart contracts contain state variables and executable code (functions).
  • When writing contracts, you should pay attention to element order.

Definition of a Smart Contract

So what is a smart contract? In its essence, it’s a computer protocol. It is called smart because of its ability to verify and execute a contract without any help from third parties. The contract exists in the decentralized Blockchain network and contains all the terms of a particular agreement.

In most cases, Solidity smart contracts are used in money-related matters. However, the actual possibilities are endless. They can be used in voting, healthcare, law, and a ton of other fields. Using smart contracts has a lot of advantages over traditional ones:

  • It saves time: instead of taking days, a contract can be verified in minutes.
  • It saves money: there is no need to pay fees to any intermediaries (such as lawyers).
  • It is safer: your documents are not only encrypted but also kept in a decentralized network. This means you always have backups.
  • It lets you avoid mistakes: there’s no need to fill in documents by hand or entrust a third party.

Smart contracts execute using a logic called If-Then. That means the results depend on a certain condition. In his writings, Nick Szabo compared it to using a vending machine: you can only get your purchase after you insert money.

Smart contracts are written in Solidity, and usually by using the Ethereum virtual machine. You can try writing your own Ethereum smart contracts in the Remix online compiler.

Note: users value Ethereum smart contracts for ensuring exact results. There is no place for human error or manipulation.

  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Writing Ethereum Contracts

When you’re writing Solidity code, you should first make sure to use a proper layout. Contracts should be at the end of it. Here is the proper order of elements:

  1. The pragma statement
  2. Import statements
  3. Interfaces
  4. Libraries
  5. Contracts

Libraries, interfaces, and contracts have their own elements as well. They should go in this order:

  1. Type declarations
  2. State variables
  3. Events
  4. Functions

You can create a contract in two ways: through an EVM transaction (externally) and inside Solidity (internally). You will find the process extremely simple if you use an IDE, such as Remix.

If you compare Solidity to other coding languages, contracts may remind you of their classes. Contracts consist of two things:

  • Persistent data kept in state variables
  • Runnable functions that can modify state variables

Note: unlike in other languages, you don’t need to use the keyword this to access state variables.

EVM function call means calling a function from a different contract. It changes the context, and the state variables can no longer be accessed.

All Ethereum contracts have an individual address in the EVM blockchain. They also support inheritance, meaning one Ethereum contract can inherit elements from others.

Tip: You can create a new contract and send Ether at the same time by using the keyword new.

Example Contract Explained

Here we have a simple example of a smart contract:

pragma solidity ^0.4.7;

contract SimpleSample {
    uint stateVar;

    function set(uint x) public {
        stateVar = x;

    function get() public view returns (uint) {
        return stateVar;

In the first line, you can see the version pragma. It informs us this contract will compile with any compiler version between 0.4.7 and 0.4.25.

The line uint stateVar; declares a state variable called stateVar. We can also see it is of the uint type, which means it is an unsigned integer of 256 bits.

You can call functions to modify stateVar, or query it. In the example, you can see two functions defined:

  • set changes stateVar value
  • get retrieves stateVar value

There are more elements that Solidity smart contracts can include. See them explained in the table below:

Name Definition
Events Convenience interfaces with a logging feature
Structs Custom defined types used to group multiple variables
Enums Used to create custom types with finite value sets

When naming smart contracts, functions, and variables, use the ASCII character set. You can also keep UTF-8 encoded data in strings. However, you should be careful when using Unicode, as it can cause encoding issues.

Solidity Smart Contracts: Summary

  • Ethereum contracts are called smart because they can execute without third parties (for example, lawyers).
  • The most important elements of any smart contract are state variables and functions.
  • To make sure your smart contract executes, list its elements in the right order.