Hello, everyone! I’d like to welcome you to the newest community blog entry of the Hello World in Every Language series. Today’s post will cover Hello World in Solidity, a contract-oriented programming language used to write and implement smart contracts invented in 2014.
Table of Contents
- Solidity Background
- Hello World in Solidity
- How to Run the Solution
- Sample Programs of Every Language
Solidity Background
To understand Solidity, it is imperative to comprehend the context in which it is used. So, the first question to ask is: what is a smart contract?
Smart Contract
According to Wikipedia, a smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. They allow the performance of credible transactions without third-parties.
The contracts used in this sense are the exact same as the regular contracts you’re familiar with. By removing the human factor and voiding the need for a third-party, people and business are able to save money and reduce the chance of fraud. Smart contracts are the main driving force of blockchains.
Blockchain
According to Wikipedia, a blockchain is a continuously growing list of records, called blocks, which are linked and secured using cryptography. Each block typically contains a cryptographic hash of the previous block, a timestamp, and transaction data.
Blockchain is the force driving the technology of all digital currencies. A couple of examples are Bitcoin, Ripple, Ethereum, Litecoin, Nem just to cite a few.
Blockgeeks has a great analogy which should help you understand blockchain works:
Picture a spreadsheet that is duplicated thousands of times across a network of computers. Then imagine that this network is designed to regularly update this spreadsheet and you have a basic understanding of the blockchain.
Another wonderful description provided by William Mougayar goes as follows:
Imagine two entities (eg banks) that need to update their own user account balances when there is a request to transfer money from one customer to another. They need to spend a tremendous (and costly) amount of time and effort for coordination, synchronization, messaging and checking to ensure that each transaction happens exactly as it should.
Typically, the money being transferred is held by the originator until it can be confirmed that it was received by the recipient. With the blockchain, a single ledger of transaction entries that both parties have access to can simplify the coordination and validation efforts because there is always a single version of records, not two disparate databases.
Hello World in Solidity
Without further ado, here’s an implementation of Hello World in Solidity:
pragma solidity ^0.4.22; contract helloWorld { function renderHelloWorld () public pure returns (string) { return 'Hello, World!'; } }
While the format of Solidity looks a bit different from the more popular programming languages today, what’s happening behind is fairly straightforward.
First we import the version of Solidity we’d like to use. Then we create a function and specify we’d only like to return a string. And, voila!
How to Run the Solution
If you want to run the solution, remix provides an IDE you can visit to write and execute the smart contract. Every piece of code written in Solidity—or any blockchain programming language—is considered a smart contract.
Sample Programs of Every Language
Thanks again for stopping by! If you hadn’t noticed, this article was contributed by a community member, Maximillian Naza. If you’d like to help contribute to this series, consider reaching out. We’re always looking for new authors.
Oh, and if you really enjoyed this article, don’t forget to give it a share. By spreading the word, you’ll help grow a fun collection of code snippets.
Until next time!
Recent Posts
While creating some of the other early articles in this series, I had a realization: something even more fundamental than loops and if statements is the condition. As a result, I figured we could...
Today, we're expanding our concept map with the concept of loops in Python! Unless you're a complete beginner, you probably know a thing or two about loops, but maybe I can teach you something new.