Solidity Hello World

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

Welcome to this tutorial on how to write your first “hello world” program in Solidity!

Solidity is a programming language that is used to create smart contracts on the Ethereum blockchain. In this tutorial, we will go through the steps to create a simple Solidity program that will print “hello world” to the screen.

By the end of this tutorial, you will have a solid understanding of the basics of a Solidity Hello World contract, and be well on your way to creating your own smart contracts. Let’s get started with the code:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract HelloWorld{
    public string hi = "Hello World";
}

How to Compile and Deploy this Smart Contract

We’ll be running this code on a virtual machine, which means it’ll be free, and once we close our web browser, it’ll disappear.

To run this smart contract, we have to both compile and deploy it. Compiling is a term that means to turn our Solidity language into bytecode, so that a computer (specifically the EVM) can process it. Deploying is a term that means to take the bytecode and actually put it on the blockchain.

Here are the simple steps to compile and deploy this smart contract:

  1. Visit https://remix.ethereum.org
  2. Create a new file named “HelloWorld.sol”
  3. Paste in the file
  4. Compile your program by pushing CTRL + S
  5. Click on the “Deploy” tab and deploy to the virtual machine

If you need a better walkthrough of how to do this, here’s a more in-depth tutorial:

Here is a breakdown of what each of those lines means:

//SPDX-License-Identifier: MIT

This line tells any other developers that the code in this file is free to use, copy, and reproduce. The specific license is called the “MIT” license, and you can read more about it here.

This line of code is a comment, as shown by the two forward-slashes in the beginning of the line.

pragma solidity 0.8.13;

This line of code tells the compiler that we are writing code that conforms to Solidity version 0.8.13 and to compile it with those specific rules. If you want to learn more about the versions of solidity, you can learn more about this line of code in our pragma guide.

Notice how this is the first line with a semicolon. The semicolon indicates the end of a line of code.

contract HelloWorld{

This line of code starts a new contract, with the keyword “contract”. The word that immediately follows it is the name of the contract, and you can see in this case we’ve capitalized both words to make them easier to read. You’ll also notice that the line ends with a starting curly bracket. This means anything from the starting curly bracket to the ending curly bracket is part of the HelloWorld contract.

string public hi = "Hello World";

There is a lot to unpack in this line of code. The first thing we’ll notice is the word “string”. String means we are about to create a new variable that is of the type “string”. Strings hold together numbers and letters.

Next, we have the word “public”. Immediately after we declare what type of variable we are creating, we need to declare the visiblity of that variable. In other words, who can see our variable? In this case, public means anyone can, including other people and other contracts.

After that, we have “hi”, and this is our variable name. Any time in the future that we refer to the variable named “hi”, we will retrieve the value of this variable.

Finally, we have an equals sign that is followed with Hello World in quotes. This means we are setting the variable named “hi” equal to “Hello World”.

}

Finally, we have the ending curly bracket, which signifies we’ve ended our smart contract code and have finalized our Hello World program.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.