In the previous tutorial of our Blockchain Roadmap, we studied the Ethereum platform, Smart Contracts and DApps. In today’s Solidity tutorial for beginners, we will see how Solidity smart contracts are structured. We will also write our first ‘Getter Setter’ smart contract.

What is Solidity?

Solidity is an object oriented high level programming language for writing smart contracts for the Ethereum blockchain. The language was put together by Gavin Wood, one of the co-founders of Ethereum. Solidity is statically typed, turing complete and is highly inspired from C++ and JavaScript.

Solidity is developing very quickly with many changes taking place in the language syntax and working over the last few years. The latest version of Solidity is 0.6.x. We will use the version 0.6.0 for this tutorial of Solidity for beginners.

How does it work?

The Solidity compiler converts high level code into bytecode that is executed by the Ethereum Virtual Machine(EVM). The EVM exists on every node of the blockchain network and runs the transactions and smart contracts on that node.

We use Solidity to code smart contracts. Simply put, a smart contract is the business logic that defines the working of the decentralized application (DApp). This smart contract is then deployed on the Ethereum Mainnet (the Ethereum blockchain) for production use. The extension of solidity smart contract files is .sol .

Structure of a Solidity Smart Contract

Structure of a Solidity Smart Contract

Before we begin writing our first Smart Contract, we must understand the structure of a smart contract. The smart contract in the image begins with the statement pragma solidity ^0.6.0; This statement is simply used to define which version of the Solidity compiler is to be used to compile the code. In our case, we will use version 0.6.0 to compile our smart contract.

Next, we name our smart contract. We do this by writing our contract name next to the keyword contract.

Inside the smart contract, we have a bunch of things like modifiers, mappings, variables and functions, etc. We will cover these in great depth in our Blockchain Roadmap. For today, we only need to know what variables and functions are.

Variables and Functions

Variable Declarations: Variables in Solidity are of two types: State Variables and local variables.

  • State variables are variables whose values are permanently stored in the contract storage. They are declared outside the functions.
  • Local variables, on the other hand, are defined inside functions. Their values are not saved between different function calls.

Functions: Functions are executable units of code inside a contract. Functions are reusable and can be called multiple times in an execution environment. Functions have different visibility, but for this Solidity tutorial for beginners, we will stick to public visibility of functions. Public visibility ensures the function can be called from anywhere.

Based on what we have understood so far, we will now code our first simple ‘Getter Setter’ smart contract.

Our First Smart Contract

Getter Setter Smart Contract

The first line of code declares the solidity compiler version to be used, i.e. version 0.6.0.

Then, we name our smart contract as getterSetterExample. In this contract, we declare a string variable firstString and assign it the initial value of “Hello World”. This variable is defined outside the function, and is therefore a state variable.

Next, we will write a function getString() that will fetch the value of our string and return it. Notice the visibility of this function is public, which ensures the function can be called from outside the contract as well.

One more function we will write is the setString() function. The setString() function will be used to overwrite or  assign a new value to our variable firstString. This function takes a memory variable newString as parameter and assigns the value passed in newString to the firstString variable.

So that’s it, we’ve successfully managed to code our first Smart Contract in Solidity. In the next tutorial of our Blockchain Roadmap, we will get familiar with Remix IDE, the online IDE for writing Solidity Smart Contracts. We will also deploy our Getter Setter smart contract using Remix.



2 Responses

  1. Nice blog here! Also your web site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol Bea Carlin Garlanda

  2. whoah this weblog is excellent i really like reading your articles.
    Stay up the great work! You recognize, lots of persons
    are looking round for this information, you could aid them greatly.

Leave a Reply

Your email address will not be published. Required fields are marked *