Web Programming TutorialsSolidity – A Blockchain Programming Language

Solidity – A Blockchain Programming Language

What is Solidity?

Solidity is a high-level programming language used in the execution of smart contracts. It has a similar syntax to C++, Python and mostly JavaScript. It runs on a specific type of platform called Ethereum Virtual Machine. All the Blockchain programs and smart contracts are executed in EVM only.

It was developed by Gavin Wood in 2014. It is a statically typed programming language which uses a .sol extension.

We could write the programs in the languages with which we are familiar like JavaScript or Python but we do not prefer these languages. This is so because as we know, we are running the smart contracts on Ethereum Virtual Machine which can only decipher Ethereum specific bytecode. This bytecode has to be lightweight because we are running the program on a virtual machine and not on a real hardware machine, so to make it easy we use solidity.

Ethereum provides a platform for decentralized applications. In computer terms, Ethereum is a ‘Turing Complete’.

Tools to develop Solidity Code:

A text editor is needed to write the program. In Solidity, we use Editors like Atom, Sublime, Visual Studio, etc. For those of you who do not want to take the trouble of actually installing the compiler, solidity offers “remix” which is an online IDE for solidity. It could be installed but mostly it is used as an online version.

The basic Layout:

pragma solidity ^0.4.23; // function used to describe the version of solidity. It only works in version 0.4.0 and up.
contract SmartContract // define a contract

The operators that are used in solidity are very similar to that of JavaScript.

Comparision Operators: <>, ==, !=, <=, >=
Bitwise Operators: &, |, ^
Arithmetic Operators: +, -, /, *

Basic Example of a Calculator (addition and multiplication using solidity)

pragma solidity 0.4.23;
contract BasicCalculator{ //contract is the highest object that cab created in Blockchain
uint public a = 10;// uint are state variables which are permanently stored in a contract storage
uint public b = 5;

uint sum = add(a,b);
uint prod = mul(a,b);
function add(uint, uint) returns(uint) // units which are executable are called functions
return a+b;
function mult(uint, uint) returns(uint){
return a*b;

Structure of a Contract:

As we have discussed above structures in a contract is very similar to that of class in JavaScript. Each of the contracts comprises of these basic declarations:

  • State Variables like ‘uint’
  • Functions which are executable units of code
  • Function Modifiers are used to change the definition of functions
  • Events are interfaces which provides facilities like EVM logging

A basic program which stores the values in the Blockchain:

In this program, we will define the basic variables using the contract ‘MyFirstContract’ in which we input the age and the name in the Blockchain using functions ‘get’ and ‘set’ and some variables. It gets stored in the Ethereum Blockchain, unlike a programming language where instance get stored in memory. Now, this program can be accessed from anywhere in the world.

program of blockchain

Inheritance in Solidity:

Multiple Inheritances is supported by solidity which includes polymorphism. In this only one contract gets executed while the code from all the other contracts gets inherited into the final contract.

With solidity, we look into three aspects of inheritance firstly being a general extension of functionality from one contract to the next contract. In the following program, we do not want the outside world to access the ‘MyInternalValue’ other than ‘MyFirstContract’ as declared in above program so we declare a value in “private”.


In this program, we will add some value to “MyFirstContract” through a bank contract. We create a function by which we can deposit an amount in the Bank, withdraw it and also check how much balance is left.

create a function

Creating Library:

As you continue to develop contracts in the Ethereum network, you start to write the same codes several times. So to prevent this copy and paste every time, it is more efficient to create libraries that you can import in a new contract. Below is an example if a library function which can be called in any program. This program adds or removes a member from a group.

pragma solidity ^0.4.0;
library Groups{
struct Group // it allows you to define your own type of objects to create attribute. Here ‘bool’ is an attribute.
mapping(address => bool) members;
function addMember(Group storage self, address addr) returns (bool) { 
if (self.members[addr]){
return false; // already a member
self.member[addr] = true;
return true;
function delMember(Group storage self, address addr) returns (bool) {
if (self.member[addr]) 
return false; //not a member
self.member[addr] = false;
return true;

Now, this library function has been created and can be used. It can be called from any other program using the keyword Groups.

Events in Solidity:

An event allows contracts to return values to the user interface here an event function comes into action. Alternatively, if you want to create some synchronous trigger with data of your smart contract then an event is written in a program. They can also be used as a log in the system.

Events in Solidity

Transaction in Solidity:

In the following example we will implement a simple cryptocurrency where we can send and receive coins from anyone. One can create a coin also but only the creator can do that.

pragma solidity ^0.4.0;
contract cryptocurrency {
address public miner;
mapping (address => uint) public Balance;
event Sent(address from, address to, uint sum);
function Coin() {
miner = msg.sender;
function mine(address receiver, uint sum) {
if (msg.sender != miner) return;
Balance[receiver] += sum;
function send(address receiver, uint amount) {
if (Balance[msg.sender] < sum) return;
Balance[msg.sender] -= sum;
Balance[receiver] += sum;
Sent(msg.sender, receiver, sum);

Alternatives to Solidity

Alternatives to Solidity

There are very specific languages that can be run on EVM. The alternatives are:

1. LLL- It is based in LISP which is another programming language. It was the first language which was used in Smart Contracts because it is lightweight to run on EVM.
2. Serpent- It is influenced by python and uses .se extension. It was popular before but due to security breaches, it is no longer in use.

The language which you should be aware of before programming in Solidity is Java because it is easily available and it is totally based on classes similar to that of Ethereum is a Smart Contract.

So, this was basically a small introduction to Solidity which allows us to write smart contracts in Blockchain.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -