spot_img
21.2 C
London
spot_img
HomeblockchainSolidity Tutorial — Solidity Programming For Beginners

Solidity Tutorial — Solidity Programming For Beginners

Codezeros

Modern tech is dominated by software- from our work applications to social media, everything runs on some complex lines of codes. Blockchains also use programming languages for creating smart contracts and decentralized apps. The most famous of them- Ethereum, has gone a step beyond and created its own programming language called Solidity. As Ethereum is one of the most used smart contract platforms, a huge amount of Blockchain Programming happens over Solidity.

In this article, we provide a beginners’ guide to solidity programming for the Ethereum Virtual Machine. We would be covering the fundamental building blocks of Solidity, its layout, where it stores the data, the common value types, operators, and data structures.

At its core, Solidity is a contract oriented language and resembles the syntax of JavaScript. It is used to execute code on Ethereum Virtual Machine (EVM) for blockchain development. The main feature of the solidity compiler is that it converts high-level codes into machine-level codes, which are simpler to understand.

Contracts are the starting point of any project and contain all variables and functions. Hence, it is the most fundamental building block. Contracts closely resemble classes used in object-oriented languages.

The layout contains pragma directives, import directives, and contract definitions. All this information is stored in a source file.

It is used to define and restrict the compiler version which can run the code. Version pragma works like a security feature to make sure that the code is executed exactly in the way it is intended. A change in semantics would also need a change in syntax and past or future versions could misrepresent the current use of the code.

Blockchain Solidity doesn’t hold the concept of a ‘default exporter’ but supports import statements, from ES6 onwards, that are similar to those in JavaScript. All global symbols from “filename” can be imported into the current global scope, however, it is advisable to import specific symbols individually.

Solidity supports both single-line and multi-line comments. It also contains another type of comment known as natspec comment. A natspec comment is used directly above a statement or a function declaration.

Before we move ahead, it is imperative to understand how and where Ethereum keeps its data.

Ethereum can store its data in the following three crucial areas:

Each account stores certain data in the storage area. The storage area stays persistent between function calls and transactions. The most important feature of the storage area is that the contract cannot read or write any part except its own. However, storage is comparatively costly to initialize and modify, let alone read.

Memory is used to hold temporary values. It gets erased between function calls and there is no requirement to expand it periodically. Hence, it is cheaper to use than storage but its cost would increase the larger it grows.

All computations are performed in the stack area. It holds small local variables and costs close to nothing. However, the stack area can only hold a limited amount of values during a time and, if required, the values have to be shifted to the memory or storage area.

Boolean

There are only two possible values in a boolean: true or false. A number of logical and equality operators can be used to create a boolean.

Integers

In Solidity, signed and unsigned integers of various sizes are possible. Comparison, bit, shift, and arithmetic operators can be used to retrieve information.

The rules applicable in Solidity are more or less similar to other programming languages.

Arithmetic

Solidity uses simple math operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus or remainder (%).

Bitwise

Used on two complementary representation of a number. For example, ~int256(0) == int256(-1)

Logical

The most common logical operators are: logical and, logical or, logical negation, equality, or inequality.

Incremental

Incremental and decremental operators in Solidity uses a prefix or suffix of either — or ++. For example, a++ and a+=1.

Structs

Structs are used to group multiple variables and works as a way to define new types in the form. It allows one to create complex data types holding multiple properties. A struct can only have 16 members, beyond that it presents an error.

Arrays

Arrays help in creating a collection of structs. These arrays can be a dynamic or compile-time fixed size.

Mappings

Mapping helps in locating each key to the whole byte-representation. Mappings are hash tables which are virtually initialized.

Ethereum Solidity Development has become a leading field in blockchain development. This guide can help early coders get broad insights into solidity programming and experienced coders understanding the similarities between Solidity and their current language. With the expected launch of Ethereum 2.0 in the coming weeks, Solidity is expected to gain more traction in the market.

Solidity Tutorial — Solidity Programming For Beginners

Source

spot_img

latest articles

explore more