DEV Community

Cover image for Solidity - Hello World
Shubham Sharma
Shubham Sharma

Posted on

Solidity - Hello World

Introduction to Solidity

Solidity is an object-oriented programming language created by Ethereum Network Team for developing smart contracts in Blockchain.

Solidity is a high-level language that is based on other programming languages, including C++, Java, Python etc. Solidity code should feel familiar if you're familiar with any of those programming languages.

Solidity has very good documentation, you can it out here.

Prerequisites for learning solidity

  • Basics of Blockchain
  • Ethereum basics
  • Basics of any programming language (eg. C, C++, Java, JavaScript, python etc.)

Where to write solidity code

In this tutorial, we will write our solidity program in remix IDE, an open-source online IDE that provides a solidity compiler.

Step 1

Click here to open Remix. You will see an interface like this.

Remix IDE interface

Step 2

Let's create a HelloWorld.sol file, in which we will be writing our first solidity code.

Create a new file in remix
Then click on this New File button and create a new file, make sure it is ending with .sol. You will find this layout a little bit similar to the visual studio code.
Create a HelloWorld.sol file

Step 4

Now we are good to go, let's write some code.

// SPDX-Liscense-Identifier: MIT
pragma solidity ^0.8.9;

contract HelloWorld{
    string public greet = "Hello World";
}
Enter fullscreen mode Exit fullscreen mode

I will explain every line of the code later in this blog, you just copy this to your remix compiler.

Step 5

Now let's compile then deploy and then run the code.

  • Compile
    Remix compile section to compile solidity code

  • Deploy
    Remix deploy section to compile solidity code

  • Run
    Remix run section to compile solidity code

Explanation of the code

// SPDX-Liscense-Identifier: MIT
Enter fullscreen mode Exit fullscreen mode

The above comment is added to prevent throwing errors and to prevent legal problems regarding copyright, it is important to specify the license of the smart contract, the MIT license makes the code available as free and open-source.

pragma solidity ^0.8.9;
Enter fullscreen mode Exit fullscreen mode

The above line means that the code will compile with a compiler greater than version 0.8.9.

We are creating a contract named HelloWorld.

contract HelloWorld [
    // ...
}
Enter fullscreen mode Exit fullscreen mode
// Here we have initialised a state variable named greet.
string greet = "Hello World";
Enter fullscreen mode Exit fullscreen mode

Above we have created a state variable with name greet, but now we want to create a function name greet which can be called by anyone outside the contract, and it should return a string “Hello World”.

Do not worry if you get confused with this syntax, You will learn everything in deep in the upcoming blog. This blog is just to give you an overview of the smart contract and solidity.

// Method 1
string message = "Hello World";
function greet() public view returns(string memory) {
    return message;
}

// Method 2
string public greet = "Hello World";
Enter fullscreen mode Exit fullscreen mode

Congrats, You have successfully written your first program in solidity.

Feel free to connect with me on Twitter, LinkedIn

Top comments (0)