Hello World! An Introduction To Solidity Smart Contracts

Hello World!

Solidity is a high level programming language used to interact with the Ethereum Blockchain with what are called "smart contracts". These contracts store the logic intended to run on the Ethereum Virtual Machine (EVM) and can modify the state of accounts on the blockchain. It is similar in looks to other coding languages such as JavaScript or C++ and supports inheritance and libraries, among other things.

With this blog post, we are going to write a basic smart contract. The function of this contract will be to say "Hello World!", a classic first challenge when learning a new programming language. All the code will be written in an online text editor called Remix so as long as you've got an internet connection (if you're reading this, I assume that you do) you will be able to follow along.

What We Will Learn

  • Basic structure of a Solidity smart contract

What You Will Need

  • Access to the internet
  • An insatiable drive for knowledge

Other Resources

Let's Get Started

Our first step will be to navigate to remix.ethereum.org. Remix is an open source code editor that can be run from a browser or locally on a desktop. It has built in functionality for writing, testing, debugging and deploying smart contracts and is a great place to play around while learning Solidity.

Under the default_workspace heading on the left, you will find a button to create a new file. Click it and let's name our new file HelloWorld.sol. This will open up the editor and give us a space to write our code. In case it doesn't automatically open up the editor, just click on the newly created HelloWorld.sol file and you should be good to go.

Click Create New File button and name it HelloWorld.sol

Click the Create New File button and name it HelloWorld.sol

The Code

Next we will get to the good stuff.. Writing code! Since the code for this project won't be too complicated, we'll write it all out then test it and then I will break it down line by line to explain what everything means.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.7;

contract HelloWorld {
    function sayHello() public pure returns (string memory) {
        return "Hello World!";
    }
}

Awesome! You have just written your first smart contract! Definitely something to be proud of! Okay so now how do we check if it works?

First, hit ctrl + s to save your work. You should notice a little green check mark on the left hand side of your screen on the Solidity Compiler icon. This is just signifying that everything compiled without warnings or errors.

Next, click on the icon below that one labelled Deploy & run transactions . You should see the name of your contract, HelloWorld - HelloWorld.sol, under the Contract Heading and directly underneath that, an orange Deploy button. Click that button and your contract should then appear under the Deployed Contracts heading at the bottom.

You can see the details of the deployment of your contract in the built in console if you like. It will tell the address deployed to and gas cost, among other things

Click on the little arrow beside HELLOWORLD to expose our sayHello function.

Clicking on the little arrow beside HELLOWORLD under Deployed Contracts will open up a dropdown menu which exposes a nice little button with our function name on it sayHello. Click that button, and VOILA! The fruits of our labor! The contract just returned a string with the contents of "Hello World!"

Hello World!

Congratulations! You have successfully written and deployed your first functioning smart contract! Now that we can see what a basic smart contract looks like and that it is working properly, let's break the code down to get a better understanding of what's going on.

The Code Step By Step

The very first line in our smart contract is the following:

// SPDX-License-Identifier: UNLICENSED

As of Solidity version 0.6.8, the first line in every smart contract is the SPDX License Identifier. SPDX stands for Software Package Data Exchange. The two forward slashes are how you comment out a line so it's not treated as part of the contract code.

Having the source code available helps establish trust that the contract isn't doing anything it's not supposed to. It is best practice to include a type of license that the code you are writing can be used under. Since this is just a quick example and we won't actually be publishing this contract we aren't going to supply an open source license. In this case it is good practice to substitute the license for the term UNLICENSED. The compiler will give you a warning if you don't have an SPDX License Identifier and will throw an error if you have more than one.


The next line in our contract will be:

pragma solidity ^0.8.7;

This line is used by the compiler to check if the source code is compatible with its (the compiler's) current version. This directive is always local to the file it is written in, meaning you would have to add another pragma directive to every other Solidity contract in your project.

The portion ^0.8.7 is telling the compiler that this contract is compatible with versions from 0.8.7 up to but not including 0.9.0. More complex rules for the compiler version can be specified, following similar syntax rules used by npm. These can be found here → https://docs.npmjs.com/cli/v6/using-npm/semver

The last piece of that line is the semicolon ;. Similar to other coding languages, a semicolon is used to indicate the end of a coding statement.


The next line is:

contract HelloWorld {

This is defining our contract and is closed off by the curly brace } at the bottom of our code on line 8. Everything inside of these curly braces is an element of our smart contract and will reside at a specific address on the blockchain. This can include functions and data related to its state on the blockchain called state variables.


Inside of our contract declaration, the first line is:

function sayHello() public pure returns (string memory) {

You will notice it is indented from the left, this is to promote readability and state that this code block is inside of the contract HelloWorld. Like our statement defining our contract, this function is also closed off by the other curly brace on line 7.

There is a lot going on in this line of the contract so let's break it down

  • function is how we state that the following code will be a function. It will then be able to be executed as such by calling it elsewhere in our code. The Remix IDE also picks up on these functions and creates a button for us to more easily test them.
  • sayHello() is the name of our function and inside the parenthesis is where we would put any arguments for the function, which is beyond the scope of this article.
  • public means that this function is accessible to the public, anyone can execute it. It's opposite would be private which means that the function is only able to be called by code in the contract itself.
  • pure is the type of function. It means that this function doesn't read or modify the state of the contract. Other keywords such as view, external, payable and others all have different uses.
  • returns (string memory) indicates that we want this function to return a string (an array of characters such as a sentence) and that we want to store it in memory as a temporary value. Memory variables are erased between function calls.

The line inside of our function is:

return "Hello World!";

This line is where the magic happens. It is telling our contract to return the string "Hello World!" whenever the function sayHello() is called. When declaring a string, it is important to wrap it in quotation marks. This expresses the boundary of the string so it doesn't get mistaken with the rest of the code.


I hope this article has helped you understand the basic structure of a Solidity smart contract. It might be a little more in depth than your usual "Hello World!" post but I wanted to make sure you understand what is happening instead of just copying the code that was written.

In my next article we will go over what a state variable is and change "Hello World!" into anything we want! This new state will be saved to the blockchain so anyone can see what we have our variable set as. How exciting!