Open In App

Solidity – Basic Syntax

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is a programming language specifically designed for developing smart contracts on the Ethereum blockchain. It is a high-level, statically-typed language with syntax and features similar to those of JavaScript, C++, and Python. Solidity is used to write self-executing smart contracts that can control the transfer of cryptocurrencies, digital assets, or other valuable items based on predefined rules and conditions.

Solidity supports various data types such as integers, strings, booleans, and arrays, and allows for control structures such as if-else statements, loops, and switch statements. It also supports object-oriented programming features such as inheritance, polymorphism, and encapsulation. Solidity contracts can be compiled into bytecode and deployed on the Ethereum blockchain for execution, providing a secure and decentralized way to execute code without the need for a central authority.

Basic Syntax of Solidity

Let’s start with a basic solidity program to understand the syntax of Solidity:

Solidity




// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract HelloGeeks {
   uint a;
   function set(uint x) public {
      a = x;
   }
   function get() public view returns (uint) {
      return a;
   }
}


1. Compiler Directive

A program is a compiler directive in programming languages that provides additional information to the compiler or interpreter. It helps to control the behavior of the code, such as language version, optimization options, or warnings.

In Solidity, pragma is used to specify the compiler version that the code should be compiled with. This is important because Solidity is a rapidly evolving language, and new versions may introduce breaking changes or improvements that affect the behavior of the code.
For example, the given code includes a pragma directive that specifies the version of the Solidity compiler that should be used:

pragma solidity ^0.8.0;

The ^0.8.0 part of the pragma directive specifies that the code should be compiled with a Solidity compiler version equal to or higher than 0.8.0, but less than 0.9.0. This ensures that the code is compatible with the latest version of Solidity, but does not break if a new version with breaking changes is released.

2. Contract

In Solidity, a contract is a collection of functions and variables that are written to be deployed on the Ethereum blockchain. A contract can be thought of as a self-contained program that can interact with other contracts and the outside world. The line “contract HelloGeeks” declares a contract HelloGeeks.

Example:

contract HelloGeeks
{
     // do something
}

3. Variables

Variables in Solidity are used to store data that can be used later in the program. There are different types of variables like uint (unsigned integer), string, bool (boolean), etc. The line “uint a” declares a variable a.

Example: 

uint a ;

4. Functions

Functions in Solidity are similar to functions in other programming languages. They are used to perform specific tasks or operations. The line “function set(uint x) public” and “function get() public view returns (uint)” declares 2 functions to set and get the value of the variable a.

Example:

function set(uint x) public {a = x;}
function get() public view returns (uint) {return a;}

5. Control Structures

Control structures are used to control the flow of the program. Solidity supports various control structures such as if/else statements, loops, etc. For example:

if (myNumber > 5) {

   // do something

} else {

   // do something else
}

for (uint i = 0; i < 10; i++) {

   // do something repeatedly

}

In the example above, we have an if/else statement that checks if the value of myNumber is greater than 5. If it is, it performs the code in the first block, otherwise, it performs the code in the second block. We also have a for loop that runs 10 times and performs the code in its block.

6. Importing Files

In programming, importing files is a way to reuse code from other files or libraries. This can help to reduce code duplication and improve modularity. In Solidity, files can be imported using the import keyword.
For example, the following code imports a file named ‘MyLibrary.sol’:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./MyLibrary.sol";

contract HelloGeeks {   // ...}

The import “./MyLibrary.sol” statement imports the MyLibrary.sol file located in the same directory as the current file. The code in the imported file can then be used in the current file.

7. Reserved Keywords

Reserved keywords are words that are reserved by the programming language and cannot be used as identifiers (such as variable names or function names). In Solidity, some reserved keywords include: 

Keyword Explanation
abstract  Indicates that a contract or function is incomplete and must be implemented by a child contract.
address A 20-byte Ethereum address.
bool A boolean value (true or false).
break Exits a loop or switch statement.
bytes A dynamic byte array.
bytes32 A 32-byte array.
constant  Indicates that a function does not modify the contract state.
contract Defines a smart contract.
enum A user-defined type that can only have a certain set of values.
event A way to log an occurrence in the contract.
external Indicates that a function can only be called from outside the contract.
function Defines a function.
if  A conditional statement.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads