Introduction to Solidity
Solidity is a high-level programming language designed specifically for
writing smart contracts that run on the Ethereum blockchain and other
blockchain platforms that support the Ethereum Virtual Machine (EVM).
Solidity was proposed in 2014 by Gavin Wood, one of Ethereum's co-founders,
and developed by the Ethereum project's Solidity team, led by Christian
Reitwiessner.
Features of Solidity
Here are some key features and aspects of Solidity:
Object Statically Turing Event
Error Handling
Oriented Typed Complete Logging
Alt text: Main Features of Solidity
1. Statically Typed: Solidity is a statically-typed language, meaning the type
of each variable ((e.g., uint256, string, address) is known at compile-time.
This helps catch errors early in the development process.
2. Turing Complete: Solidity can theoretically solve any computational problem,
given enough time and resources. This feature allows developers to implement
complex logic and algorithms within smart contracts, enabling a wide range of
decentralized applications (DApps) to be built on blockchain platforms. For
example, it can handle tasks like managing multi-step financial transactions,
automating voting systems, or creating decentralized exchanges.
3. Event Logging: Solidity allows smart contracts to emit events, which can
be captured and used by decentralized applications (DApps) to trigger
further actions.
In Solidity, events are a way for smart contracts to communicate
information to external applications or users. They serve as a form of
logging within the Ethereum blockchain, allowing smart contracts to emit
notifications about specific actions or changes in the contract's state.
1|Page
4. Error Handling: Solidity includes robust error handling capabilities with
constructs like require, assert, and revert.
5. Object-Oriented: Solidity, while primarily designed for writing smart
contracts on the Ethereum blockchain, incorporates OOP principles to
facilitate the development process.
Here's how Solidity utilises OOP:
● Contracts as Classes: Solidity's contracts act like classes in traditional
OOP languages, containing both data (state variables) and functions.
● Inheritance: Contracts can inherit properties and methods from other
contracts, promoting code reuse and modular design.
● Visibility Modifiers: Solidity provides modifiers like public, private, internal,
and external to control access to state variables and functions, enabling
encapsulation.
● Function Overloading: Solidity allows multiple functions with the same
name but di
● Polymorphism: Different parameter lists within the same contract, akin
to polymorphism in OOP.
● Abstract Contracts and Interfaces: These features allow developers to
define standards and ensure adherence, enhancing interoperability
and code reliability.
Getting Started with Solidity Programming
The Solidity programming language offers several basic constructs that
facilitate the creation of contracts. Below are the fundamental constructs:
a. Compiler Directive
b. Contract
c. Reserved words
d. Variables
e. Functions
f. Control Structures
2|Page
These constructs form the foundation for developing smart contracts in
Solidity, enabling developers to define the behavior and logic of their
decentralized applications.
a. Compiler Directive: Compiler directives, referred to as pragma directives
in Solidity, are instructions positioned at the start of a Solidity source file to
define compiler settings. The most common pragma directive is pragma
solidity, which designates the compiler version to be employed.
Example
pragma solidity ^0.8.0;
The example above specifies that the contract necessitates a compiler
version of 0.8.0 or later, with the ^ symbol indicating compatibility up to,
but not encompassing, version 0.9.0.
b. Contract: In Solidity, a contract is a fundamental building block that
encapsulates code and data, deployed and executed on the Ethereum
blockchain. Contracts define rules and functionalities, representing digital
agreements. For instance, a simple contract might manage the transfer of
digital assets between users on the blockchain. Here's a basic example:
contract demo
{
// do something
}
1. contract demo: This line starts the definition of a smart contract
named demo. The keyword contract is used to define a new contract,
followed by the name of the contract (demo in this case).
2. { /* do something */ }: Inside the curly braces { }, you typically define
the functionality of the contract. This is where you write the code that
defines the behavior and data storage of the smart contract.
3|Page
c. Reserved Keywords: Reserved keywords in Solidity serve to ensure clarity,
consistency, and unambiguous interpretation of code. They help keep
code clear and easy to understand by giving each word a specific job, like
saying if to check something or function to describe a task. This makes it
easier for programmers to write code and for computers to understand it.
Keyword Explanation
Indicates that a contract or function has no
abstract implementation and must be overridden by derived
contracts.
address Data type representing Ethereum addresses.
bool Data type representing boolean values (true or false).
break Used to exit from a loop or switch statement.
Deprecated alias for view or pure functions, indicating
constant
that the function doesn't modify state.
contract Keyword used to define a new contract.
enum Keyword used to define an enumeration.
Keyword used to define events that can be emitted by
event
contracts and observed by external applications.
Special function executed when a contract receives
fallback
ether without any data.
function Keyword used to define a function within a contract.
Visibility modifier indicating that a function or state
internal
variable can only be accessed internally.
mapping Data structure representing key-value
4|Page
d. Variables: In Solidity, variables are containers for storing data within smart
contracts. They can hold different types of data such as numbers, text,
and addresses.
Syntax : type name; ‘[where type specifies the data type]
uint256 number; // Declaration of an unsigned integer
variable named 'number'
string text; // Declaration of a string variable named
'text'
address owner; // Declaration of an address variable named
'owner'
e. Functions: In Solidity, functions are blocks of code within smart contracts
that perform specific tasks or operations. They can take parameters as
input, execute code, and optionally return a result.
Syntax: function functionName(parameters) visibility { /* code */ }
● functionName is the name of the function.
● parameters are optional inputs that the function may take.
● visibility determines who can call the function. It can be one of: public,
private, internal, or external.
Example Code:
function add(uint256 a, uint256 b) public returns (uint256)
{
return a + b;
}
This defines a public function named add that takes two uint256
parameters a and b, and returns their sum as a uint256.
5|Page
You will learn about functions in detail in the later part of the course.
f. Control Structures: In Solidity, control structures are used to control the
flow of execution within smart contracts. They include conditionals (if
statements), loops (for, while, and do-while loops), and modifiers.
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
You will learn about control structures in detail in the later part of the course.
6|Page