Introduction

In this article, we're going to talk about solidity and walk you through the process of building a simple calculator in solidity.

Solidity

Here are some basic topics to familiar to you.

  1. What is Solidity and its Data Types?
  2. What is a contract in solidity?

What is Solidity?

It is a most popular high-level programming language that is widely used for developing decentralized applications on the Ethereum blockchain and is specifically designed for creating smart contracts on blockchains, particularly Ethereum. Think of it as the tool that allows to self-executing the contracts and it is also case-sensitive.

Solidity is easy for programmers to understand and it is familiar in languages like JavaScript, C++, and Python.

Data Types in Solidity

What is a contract in solidity?

In Solidity, a contract is the basic fundamental block building for creating smart contracts on the Ethereum blockchain. it's a blueprint that defines a set of rules, data storage, and functions that interact on the Ethereum blockchain. Imagine it as a self-contained program that resides on the blockchain.

In solidity, there are some benefits of using a contract.

Let's start building the calculator in solidity

Now, that you are familiar with Solidity and know the basics of Solidity, Let's learn how to make a calculator.

Select the latest version

Start with selecting the latest version of pragma solidity and add a comment.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

Create contract

Create a solidity contract with was name calculator and also add a comment.

Solidity contract

Add functionality

Let's add simple arithmetic operators for operations to our calculator.

Addition

Let’s look at the following addition function.

Addition function

Subtraction

Let’s look at the following Subtraction function.

Subtraction function

Multiplication

Let’s look at the following Multiplication function.

Multiplication function

Division

Let’s look at the following Division function.

Division function

Final Code

After compiling all of the functions inside the calculator contract, the code will look like this.

Calculator contract

Set up the Remix IDE

Let’s review how you can set up Remix by writing and deploying your first smart contract.

  1. Go to the official site of Remix Ethereum IDE, select the solidity, and create a new name calculator.sol.
     Remix Ethereum
  2. Copied the following code and pasted it to the calculator.sol file.
    Calculator.sol
  3. Go to the File Explorer and search Solidity Compiler.
     Solidity Compiler
  4. Click on the Compile and Run Script button to run the calculator contract.
    Script button

Deploy and call the calculator contract

Lastly, learn how you can deploy and call your calculator contract.

Deploying the contract

Click on the Deploy and Run Transaction button, in the File Explorer.

 File Explorer.

Deploy and Run Transaction will look like this.

Run Transaction

Clicking on the Deploy button then they will deploy the calculator contract.

Call the calculator contract

After deploying the contract, the following field will appear.

Contract

Drag the field and enter num1 and num2 inputs in multiply.

Enter num1 and num2

Click the call button and you will see a 0:uint256: 500 field giving you the output.

Call button

Repeat the processes for other calculator operations.

Summary

In this article, we have introduced Solidity and provided a step-by-step tutorial for building a simple calculator in Solidity. The article covers solidity basics, and contracts, and focuses on using the REMIX Ethereum IDE.