What every beginner should know about Solidity (2024)

Introduction to smart contracts programming language

What every beginner should know about Solidity (3)

Solidity is a programming language for implementing smart contracts on various blockchains, most notably, Ethereum. Smart contracts are programs which control the behavior of accounts within the Ethereum state. With Solidity you can create contracts for uses such as voting, crowdfunding, auctions, multi-signature wallets and many more.

Solidity is a curly-bracket language, which means it uses curly brackets to enclose blocks, as opposed to languages like Python, where blocks are defined by indentation. But Solidity documentation says that it is influenced not only by C++ and JavaScript, but also by Python. Anyway, from the syntax perspective you’ll find it close to C++ and other curly-bracket languages deriving syntax from plain old C, such as C# and Java.

If you are now scared of Solidity as a language like C++, please don’t worry, here are talking about syntax only, there’s no complex staff like manual memory management.

Below is a simple contract from the official Solidity documentation, which will demonstrate Solidity syntax:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract SimpleStorage {
uint storedData;

function set(uint x) public {
storedData = x;
}

function get() public view returns (uint) {
return storedData;
}
}

Influence from C++ can be seen in the syntax for variable declarations, loops, the concept of overloading functions, implicit and explicit type conversions, and many other details, as opposed to influence from Python, which is much less and can be seen only in multiple inheritance, super keyword, and few other features, like Solidity’s modifiers, which were added trying to model Python’s decorators with a much more restricted functionality.

You can find more details about which languages Solidity has been inspired by in the language influences section of the official Solidity documentation.

Since Solidity syntax is like C++ syntax, it has many of the same expressions and control structures typically used in curly-brace languages, like if, else, while, do, for, break, continue, and return. It also supports exception handling with try/catch statements.

Solidity is an object-oriented language, meaning that it follows OOP paradigm based on the concept of classes, which can contain data and code in a way that such classes will be self-contained and reusable entities, like a “black box”, so when using such classes, one doesn’t need to worry about its internals.

As Solidity is object-oriented, it also supports one of the basic OOP features, such as inheritance. In general OOP, inheritance is understood as a way to create a custom class on top of another base class, extending base class functionality.

In Solidity inheritance allows developers to create a contract built on an existing contract.

Here is an extract from the documentation, which demonstrates inheritance in Solidity:

contract Owned {
constructor() { owner = payable(msg.sender); }
address payable owner;
}
// Use `is` to derive from another contract. Derived
// contracts can access all non-private members including
// internal functions and state variables. These cannot be
// accessed externally via `this`, though.
contract Destructible is Owned {
// The keyword `virtual` means that the function can change
// its behaviour in derived classes ("overriding").
function destroy() virtual public {
if (msg.sender == owner) selfdestruct(owner);
}
}

As many other modern high-level languages, programs (or contracts) written on Solidity runs on a virtual machine, as opposed to languages designed to be compiled in binary code to be executed directly by a CPU.

As Java programs run on JVM, Python programs run on PVM, so does Solidity programs run on EVM, which stands for Ethereum Virtual Machine, which is the runtime environment for transaction execution in Ethereum. It includes everything you would expect for a typical virtual machine: stack, memory, program counter, and the persistent storage for all accounts, including contract code.

Solidity is a statically typed language, meaning that all variable types must be explicitly defined during declaration. That means, that there are no any “general” variable types, such as JavaScript “var” or modern C++ “auto” type.

Solidity supports three main categories of types: value types, reference types, and mapping types. Each category includes many different types, i.e., value types are integers, booleans, address type, fixed-point numbers, contract types, and more.

The concept of “undefined” or “null” values does not exist in Solidity, but newly declared variables always have a default value dependent on its type.

Libraries are like contracts but are mainly intended for reuse. A Library contains functions which other contracts can call. Solidity imposes certain restrictions on use of a Library. Following are the key characteristics of a Solidity Library:

  • Library functions can be called directly if they do not modify the state. That means pure or view functions only can be called from outside the library.
  • Library can’t be destroyed as it is assumed to be stateless.
  • A Library cannot have state variables.
  • A Library cannot inherit any element.
  • A Library cannot be inherited.

Despite having libraries, basic tooling functions and variables are just exposed as globals, rather than being stored grouped in some sort of a standard library.

This includes properties that give certain information about the blockchain, mathematical functions, decoding and encoding functions, error-handling functions, members of address types, and so on.

What every beginner should know about Solidity (2024)
Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6479

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.