Comprehensive Guide To Solidity Syntax
In this article, we are examining Solidity syntax, including data types and its functions, whether you are a beginner or an experienced developer looking to brush up on your Solidity skills, read on to learn about the building blocks of Solidity syntax.
As with any programming language, Solidity has its own syntax and set of rules that must be followed to write correct and effective code. If you are new to Solidity, learning the language’s basic syntax is an essential first step.
Solidity Syntax
A Solidity source file can contain a great number of contract definitions, import directives, and pragma directives all at the same time.
Let’s start by taking a look at a simple Solidity source file.
An example of a Solidity file can be found below:
Solidity Syntax Example: 1 
Solidity Pragma
There is a pragma directive that specifies that the source code is written for Solidity version 0.4.0 or any newer version without breaking functionality up to, but excluding, version 0.6.0.
Pragmatic directives are always local to the source file, so they will not automatically apply to the file you import.
Considering that a file that cannot be compiled earlier than version 0.4.0 will not also be able to be compiled by a compiler starting with version 0.5.0, a pragma for such a file will be written as follows:
pragma solidity ^0.4.0;
In this case, we have added a second condition by using the symbol ^.
Solidity Contract
Solidity contracts contain code (their functions) and data (their state) that reside at specific addresses on the Ethereum blockchain.
In the line ample, a uint variable named ample is declared, and its value can be modified or retrieved using the set and get methods.
Importing Files
Despite the fact that the above example lacks an import statement, Solidity supports import statements that closely resemble JavaScript import statements.
All global symbols are imported from filename in the following statement.
import "filename";
Here is an example of creating a global symbol symbolName that contains all the global symbols from the filename.
import * as symbolName from "filename";
Importing a file x from the same directory as the current file is done using import “./x” as x;. Import “x” as x will refer to a specific file; rather, a global “include directory” will reference a different file.
Solidity Reserved Keywords
A list of Solidity’s reserved keywords can be found below:
Keywords | Overview |
abstract | This keyword is used to declare abstract contracts or functions. An abstract contract or function must be implemented by a child contract or it will result in a compilation error. |
after | This keyword is used to specify a time delay after which a function or statement should execute. |
alias | This keyword is used to create a new name for a data type or variable. |
apply | This keyword is used in the context of a multisignature wallet to apply a transaction or message to a contract or account. |
auto | This keyword is used to declare local variables with automatic storage duration. |
case | This keyword is used in switch statements to specify the action to be taken for a specific case. |
catch | This keyword is used in try-catch statements to handle errors or exceptions that occur during program execution. |
copyof | This keyword is used to create a copy of an array or a storage variable. |
default | This keyword is used in switch statements to specify the action to be taken if no other case matches. |
define | This keyword is used to create a macro or a constant value that can be used throughout the program. |
final | This keyword is used to indicate that a contract or function cannot be overridden by a child contract. |
immutable | This keyword is used to declare variables that cannot be changed after initialization. |
implements | This keyword is used to indicate that a contract implements a specific interface. |
in | This keyword is used in for loops to specify the range of values to iterate over. |
inline | This keyword is used to indicate that a function should be inlined at compile time. |
let | This keyword is used to declare variables with block scope. |
macro | This keyword is used to define a macro or a constant value that can be used throughout the program. |
match | This keyword is used in switch statements to match a value to a specific case. |
mutable | This keyword is used to declare variables that can be changed after initialization. |
null | This keyword is used to indicate the absence of a value. |
of | This keyword is used in for loops to specify the type of elements to iterate over. |
override | This keyword is used to indicate that a function is intended to override a function with the same name in the parent contract. |
partial | This keyword is used to indicate that a contract is defined in multiple files. |
promise | This keyword is used to indicate that a function returns a promise or a future value. |
reference | This keyword is used to declare reference variables that refer to other variables. |
relocatable | This keyword is used to indicate that a contract or function can be moved to a different location in memory. |
sealed | This keyword is used to indicate that a contract or function cannot be extended or overridden. |
sizeof | This keyword is used to get the size of a data type or a variable in bytes. |
static | This keyword is used to declare class variables or functions that are shared across all instances of the class. |
supports | This keyword is used to check if a contract supports a specific interface. |
switch | This keyword is used to specify a multi-way branch statement. |
try | This keyword is used to specify a block of code that may throw an exception and should be handled using catch. |
typedef | This keyword is used to define a new data type. |
typeof | This keyword is used to get the type of a value at runtime. |
unchecked | This keyword is used to disable range checks and other safety features to improve performance. |
understanding the Solidity syntax is essential for writing smart contracts on the Ethereum blockchain.
While the language can be complex, mastering the various keywords, data types, and functions can lead to the creation of powerful and secure smart contracts.
Now that you have familiarized yourself with the Solidity syntax, It’s time to learn how to write your first Solidity program.