Getting started with Solidity using Remix IDE

Rishikesh Kale
5 min readApr 24, 2020

--

What is Solidity ?

Solidity is a high level language that is a combination of JavaScript, Java, and C++. It is specially designed to write smart contracts and to target the Ethereum Virtual Machine(EVM).

EVM is carrier of the protocols and smart contracts that run on an Etherum blockchain. The code written in Solidity language gets compiled to something called the EVM bytecode which gets deployed to the Ethereum blockchain.

Let’s begin our programming venture with a Hello World program

Version

Every solidity source code starts with a “version pragma”- a declaration of the version of the Solidity compiler this code should use.

This is to prevent issues with future compiler versions potentially introducing changes that would break your code.

For the scope of this program, we’ll want to be able to compile our smart contracts with any compiler version in the range of 0.4.22 (inclusive) to 0.7.0 (exclusive).
Syntax :

Contracts

Contracts in Solidity are similar to classes in object-oriented languages. They contain data in variables and functions that can modify these variables.
They are defined by using a contract keyword, followed by the contract name and and two brackets { } which will later enclose contract variables and functions.
Syntax :

Variables

State variables are used to store information on the blockchain. They can also be manipulated by the functions within the contract. Solidity is a statically typed programming language, meaning that each variable must have its type specified.
Examples of the main data types:

1. Booleans
Can only have one of the two following values : true or false.
Keyword : bool

2. Integers
signed integers : takes both positive and negative values
Keyword : int8 to int256
unsigned integers : takes only values greater than and equal to 0
Keyword : uint8 to uint256

The number signifies the maximum number of bits it can store,
and it can be any number between 8 and 256 which is multiple of 8.
The int or uint keyword alone is considered as int256 or uint256.

3. Addresses
Used to hold Ethereum addresses.
Keyword : address

4. Strings
The string variable is used to store text information.
Keyword : string

Syntax :

Constructor

This runs when the contract is executed.

Syntax :

The value of string variable yourName is initialized to “World” when the contract is compiled and run.

Function

Functions can take any number of variables as an input, and it can return a specific value back. In case of Solidity, you can even return multiple values from one function.

Syntax :

This function set allows us to set the string variable yourName to a user defined value.

If we want to return data and avoid changing the contract’s state, we can use keyword returns() ,which is used to specify the type of the function output.

Syntax :

Functions can be declared view in which case they promise not to modify the state.

The function hello returns a string value value stored in the variable yourName.

Storage Types

Solidity has two places where it can store variable data: storage (which is the blockchain itself) or memory (a temporary place which is erased when it is no longer needed). Storage is expensive to use but memory is not. By default, structs and arrays reference to storage and all other variables use memory. If you want to make sure that a specific variable is stored in memory, you need to use the memory keyword.

Syntax :

string memory variablename;

Congrats! You wrote a Smart contract in Solidity!

Setting up the Remix IDE environment

Let’s move to the remix environment to work on this example.

On the left of your browser screen you will see 4 pre-created Smart Contracts. To create a new Smart Contract create a new file named “HelloWorld.sol”.

Compiling the Solidity Smart Contract using Remix IDE

Copy paste the following code into Remix IDE in the newly created file.

pragma solidity >=0.4.22 <0.7.0;
contract HelloWorld
{
string public yourName;

constructor() public
{
yourName = "World";
}

function set(string memory name) public
{
yourName = name;
}

function hello() view public returns (string memory)
{
return yourName;
}

}

Now its time to compile and run our Smart Contract

To compile our Smart Contract click on the Compile HelloWorld.sol button.

When our Smart Contract has been successfully complied , the next step is to Deploy and Run the transaction.

Once the Smart Contract is deployed you will be able to see the functions we had created in our code.

We can also see the yourName variable apart from the functions we created. This is because it was a public variable and it has been automatically given a getter function.

Now let’s check what value does execution of hello function (button) display after compiling and deploying the HelloWorld contract ?

It displays the value “World” because we haven’t yet set the value. Let’s do that by calling the set function and setting the value of the string variable to “Solidity”.

You can see that after execution of hello function (button) it displays the value as “Solidity”.To check what value is stored in the string variable yourName you can click on the youName button.

Congrats! Now you are able to compile, run and deploy your Smart Contract in the remix environment.

Please check me out on LinkedIn as well, I would love to hear from you!

--

--