Unlock the Power of Solidity: Exploring the Essential Keywords for Smart Contract Development
Last Updated :
23 Jul, 2025
Solidity is a smart contract programming language built solely to develop smart contracts that can be deployed on blockchains. C++ and JavaScript inspired it, an Object Oriented and High-Level language (HLL), that helps developers define the behaviour and rules for a Decentralized Application (Dapp). Smart contract facilitates autonomous, secure, and efficient transaction execution on the blockchain without needing any intermediary (self-executable) and solidity provides the base framework for achieving the same.
It is essential to understand the common keywords used in solidity to solidify your core understanding of the language so in this article, we'll pin down some prominent keywords that help us develop these smart contracts by embracing the true power of solidity language.
Contract
The keyword 'contract' as indicated by its name is used to define a smart contract.
- A smart contract is a self-executing code that consists of the data members to store data, and functions that can be used to interact with it from the outside world of blockchain.
- It is similar to declaring a class in any other high-level programming language.
- There are two ways to create a contract: From solidity, and from JavaScript Api web3.js.
Example:
The below code creates a contract named 'GeekContract', that has a single storage variable named : "sayHello()"
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^8.25.0;
contract GeekContract {
string public message = "Hello Geek";
}
Output:
ContractConstructor
Constructor keyword is used to create a constructor which is a special type of function that will be executed as soon as the smart contract is Deployed to the chain, and is used to set the initial state variables in the contract and do some initilial startup work.
Note:
Constructor can only be declared using the 'constructor' keyword starting from solidity version ^0.6.0. And the old method to do so was using the Contract name which is DEPRECATED now.
Example:
The below contract code will have a public function for the state variable 'message' which will contain the string : "This is my first Contract!" and this string will be set on the deployment of this contract on chain.
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract GeekContract {
string public message;
constructor(){
message = "This is my first Contract!";
}
}
Output:
Constructor
Data Types
- address: Stores ethereum address, either a contract address or account's address.
- bool: Stores a true or false binary value.
- int or unit: int is a positive or negative value, uint (unsigned integer) is a positive whole number storage type.
- bytes: Represnts a dynamic array used for storing arbitrary binary data.
- string: It is a data type defined for storing the long sequence of alphanumeric and special characters. Mainly used for storing UTF-8 encoded characters - text data.
Delete
The 'delete' keyword is used along with the state variables or structs for resetting their values. It helps in managing the contract state and resources by clearing the data structures values, resetting states etc.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract GeekContract {
uint256 public myNumber = 10;
address public myAddress = "Input Valid Ethereum Address here";
function resetVariables() public {
delete myNumber; // Sets myNumber to 0
delete myAddress; // Sets myAddress to address(0)
}
}
Explanation:
- This code declares two public variables : myNumber = 10, and myAddress which will contain a valid ethereum address.
- Once the resetVariables() function is called, it will reset the values of these storage variables value to their default values.
Enum
The keyword 'enum' is used to declare an enumeration type consisting of only some of the selected pre-defined values that are called 'enumerators'.
- It restricts a variable type to have only certain set of pre-defined values.
- It can be defined inside a contract itself.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract TransactionState {
// * Defined enum type with 3 enumerators : active, pending, failed
enum State {active, pending, failed}
}
Explanation:
- The above code simply defines an enum type 'State' inside a 'TranscationState' contract. It has 3 enumerators : active, pending and failed.
- You can assign these values to variables of type State.
Function
A function is a set of code statements that are executed when a function is called from the inside (from another function) or outside of the smart contract.
1. The keyword function is used to declare a function followed by the function name, parameters it takes (wrapped in a parenthesis), visibility specifier like public, private, external or internal, type of function, return type (if any). Below is an example of function signature:
function functionName(parameter1 ..... parameterN) visibilitySpecifier modifier returns(datatype) () { // Code Logic Here }
2. Function Modifier: 'pure' is to prevent modification or state access, 'view' is for reading the state but preventing any modification, and 'payable' is so that functions can receive ethers when called. We'll learn more about these later in the article.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract GeekContract {
string public message;
constructor(){
message = "This is my first Contract!";
}
// Reset message variable
function sayHello () public {
message = "Hello Geek";
}
}
Explanation:
The above code is an extension of previous code, we've just added a new function named 'sayHello' which resets the value of message variable to "Hello Geek" when called.
Output:
Function
Interface
The 'interface' keyword is used to declare an interface in solidity for providing a declaring for functions that will be implemented by smart contracts.
- Interfaces increase code reusability, readability and also promotes interoperability as they can be used in any contract.
- Interfaces are a collection of function declarations (without the function definition) that will be given its implementation in the implementing classes and cannot contain anything else (like variables or modifiers)
- They cannot inherit from other contracts but other interfaces.
- All declarations functions MUST be external in the interface even if they're public
- Unlike contracts, they don't have a constructor.
Example:
Solidity
interface MyToken {
// Function Declarations
function transfer(address _from, address _to) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
Explanation:
Above is an interface declared named 'MyToken' this interface can be implemented by any contract using inheritance's 'is' keyword.
Inheritance - 'is' keyword
In the context of Solidity, Inheritance refers to the property of once smart contract to acquire properties of another smart contract. The 'is' keyword is used for two purposes:
- For inheriting smart contract.
- For implementing interfaces.
- Below is an example for implementing interface (Same can be done for another contract).
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
interface GeekToken {
// Function Declarations
function transfer(address _from, address _to) external returns (bool);
function balanceOf(address account) external returns (uint256);
}
// GeekContract implements the GeekToken interface
contract GeekContract is GeekToken {
uint256 public balance;
function transfer(address _from, address _to) external override returns (bool) {
// * Transfer logic
return true;
}
function balanceOf(address _account) external override returns (uint256) {
// BalanceOf Logic
balance = _account.balance;
return balance;
}
}
Explanation:
- You might encounter some warning as for simplicity we haven't implemented the logic
- 'GeekToken' is an interface, implemented by 'GeekContract' named contract and its overriding the functions present in the interface, by utilising function type keywords such as - 'override'
Output:
InheritanceImport
Import statements are nothing but extracting and accessing a piece of code that exists somewhere outside the current file. In solitidy, we can import from two places:
- External Solidity Library
- Another file in our same project
Example for External Solidity Library:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
Example for Importing from another file:
import "./MyOtherContract.sol";
Explanation:
This imports the File named "MyOtherContract".
Struct
'struct' keyword is used to declare a structure that consists of various different data types. It is used to declare a new 'type' in itself like a custom data type.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
struct Geek{
string name;
uint16 age;
address addr;
}
Explanation:
- The above code declares a new type called 'Geek' which has 3 fields : name, age and addr.
- This type can be used to create variables of type Geek, and organized Geek's Data.
- Note that this code will not generate any output because its a structure not a contract.
Visiblity Specifiers
Visibility specifiers are analogous to the access specifiers or modifiers in object oriented languages, they define the visibility and accessibility of the function or variables.
- public variables and functions are accessible from everywhere.
- external functions are only callable from external world outside the contract.
- internal functions and variables are accessible in current and inherited contracts.
- private variables and functions are accessible only in the current contract.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract GeekContract {
function externalFunction() external {
// can only be called from the outside
}
function internalFunction() internal {
// Only accessible in this contract and child contracts
}
function privateFunction() private {
// Accessible only in this contract
}
function publicFunction() public {
// Accessible Everywhere!
}
}
Output:
Visibility SpecifierExplanation:
As you can see, only the public and the external functions are visible in the output. Because private & internal functions are accessible on the contract, and the sub-contracts respectively.
Exception Handling
The exception handling mechanism in solidity is defined for handling cases when the error is made on the user's end like invalid inputs, or even validate a condition.
- revert: Used when we want to revert the state of smart contract back to how it was, in case exception conditions arise.
- required: It enforces a condition satisfying mechanism to ensure a certain condition is met before a piece of code is executed. (Input validation mechanism)
- assert: unlike revert and required, assert is used to pre-enforce conditions. Suitable for conditions that should NEVER Occur.
Pragma Directive
Solidity and Blockchain is an ever evolving space, that's why many updates and breaking changes are made very often compared to any other technologies that's why it offers this feature to avoid compatibility issues.
- Pragma is a directive that is followed by the version selection operator '^' and the version number. It defines the solidity compiler's version that this code will be compiled on.
- It helps avoid compatibility issues as updates are made frequently.
Example:
pragma solidity ^0.8.25;
This defines that the smart contract will use solidity version >= (greater than equal to) 0.8.25.
License Defintion
It is used to specify the license version under which the code will be distributed (made available to everyone). Every license has their own set of protocols for right to use, code modification, copy writing guidelines etc. Solidity has some of the popular license under : SPDX (Software Package Data Exchange), Apache License, MIT License, GPL & LGPL etc.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
Explanation:
The above code defines that the given source code will be licensed upon the GPL (General Public License) version 3.0 which gives the user the right to use, modify, study and distribute as well.
Modifier
In Solidity, modifiers are special type of functions that helps us add some extra functionality to the existing functions. You can define a modifier by using keyword 'modifier' followed by its name, and parameters it may take.
- Modifiers helps restrict access, add additional conditions to the existing functions without rewriting the entire function, saving extra lines of code.
- You can apply multiple modifiers to a function and pass parameters to the modifiers.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Counter {
uint256 public count = 0;
address public owner;
// Modifier definition
modifier onlyOwner {
require(msg.sender == owner, "Only owner allowed");
_;
}
// Modifier definition
modifier valueGreaterThanZero(uint number) {
require(number > 0);
_;
}
constructor() {
owner = msg.sender;
}
function increment() public onlyOwner {
count++;
}
function customIncrement(uint256 digit) external onlyOwner valueGreaterThanZero(digit) {
count += digit;
}
}
Output:
Initially, before calling the customIncrement method:
Before callingAfter calling the customIncrement method:
After CallingData Location
In solidity, for effective memory management we can decide where to store the variables based on their nature or existence duration.
- There are mainly two types of data location : 'storage' and 'memory'.
- Storage : Storage variables are persistent and are stored permanently on the blockchain, and also require more gas fee. These are similar to class data members in OOPs.
- Memory variables are temporary and are stored temporarily, they are destroyed as soon as the function execution finishes. Mainly used in the function parameters.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract GeekContract {
// Storage Variable - message
string message;
constructor(){
message = "This is my first contract";
}
// return type is a memory variable, not stored permanently
function sayHello () public pure returns (string memory) {
return 'Hello Geeks!';
}
}
Explanation
- The above code has one storage variable : 'message' which is stored permanently on the blockchain
- And the return type is of memory type data location, indicating that it is there temporarily.
Output:
Data Location
Global Variables
Some variables are facilitated for accessing the global state of the blockchain :-
- msg: for accessing the sender address, amount of ethereums received etc.
- block: for accessing meta data like timestamp, hash, difficulty, number of the block etc.
- tx: Represents the current transaction and can be used to access the initial sender, and the gasprice cost.
Fallback Function
Fallback function is a special type of function in solidity that is called when either condition is met :-
- A non-existing function is called. Example : function fun() is called from within the contract, which was never defined inside the contract's code.
- It doesn't take any parameters but must be marked as external and payable
- If ethers are sent without any function data (information regarding which function to invoke)
SelfDestruct
- It helps clearly define the contract's exact purpose and can be used to introduce a loophole or honeypot in the contract, indicating its vulnerability.
- Example: After certain amount of ethers are collected, then it will first call a transfer function to transfer all the ether to another address and then self-destruct like it never existed.
Note:
This function is DEPRECATED starting from Cancun Hardfork, but we can still use it but it will NOT Destroy the underlying opcode in the blockchain but only transfer the ethers to the owner.
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract SelfDestructExample {
address public owner;
// Set the contract deployer as the owner
constructor() {
owner = msg.sender;
}
// Function to destroy the contract and transfer remaining ether to the owner
function destroy() public {
require(msg.sender == owner, "Only the contract owner can destroy the contract");
selfdestruct(payable(owner)); // Self Destroy the contract and transfer remaining ether to the owner
}
}
Explanation:
- The given code is just an example and it would be required to implement the functionality to collect ether, so that they can be transferred to the owner upon calling selfdestruct function
- The above code will transfer the remaining ether in the contract to the owner and will self destroy and remove itself from the blockchain.
Output:
SelfDestruct
'assembly' and 'inline assembly'
Both the keywords are typically used when we're trying to define assembly level bytecode for the EVM (Ethereum Virtual Machine) directly.
- Used for code optimization.
- It requires technical expertise and advanced solidity EVM knowledge.
- Assembly language code can be written in the assembly block inside a function like below:
Example:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
contract AssemblyContract {
// Function defining the assembly block
function assemblyFunction() public pure {
assembly {
// Assembly code here
}
}
}
Explanation:
The function defines an assembly block where you can directly write assembly code.
Similar Reads
Solidity Tutorial Solidity tutorial is designed for those who want to learn Solidity programming language and for experienced Solidity developers looking to gain a deeper understanding of the language. The following Solidity tutorial explains the basic and advanced concepts of Solidity programming language and provid
6 min read
Solidity Basics
Introduction to SoliditySolidity is a brand-new programming language created by Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 and led by Christian Reitwiessner. Some key features of solidity are listed below: Solidity is a high-level programming language designed
5 min read
Setting Up Smart Contract Development EnvironmentA development environment is an environment in which all the resources and tools are available which are used to develop a program or software product. Here, an attempt to create a development environment that is a collection of the processes and tools that are used to develop smart contracts.There
5 min read
Solidity - Basic SyntaxSolidity 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 ca
5 min read
"Hello World" Smart Contract in Remix-IDEWhat do you mean by Smart Contract? Smart contracts are self-executing contracts. The term was coined by Nick in 1994. Smart contracts are very different from traditional software programs. They are immutable once deployed on the blockchain. It was because of Ethereum the term smart contract became
4 min read
Solidity - CommentsComments are an important aspect of programming as they help in providing clarity and understanding to the code. They allow developers to document the code and explain its purpose, making it easier for others to read and maintain the code. Solidity, being a programming language, also supports the us
4 min read
Solidity - TypesSolidity is a statically typed language, which implies that the type of each of the variables should be specified. Data types allow the compiler to check the correct usage of the variables. The declared types have some default values called Zero-State, for example for bool the default value is False
4 min read
Variable and Operators
Control Flow in Solidity
Reference & Mapping Types in Solidity
Solidity - StringsSolidity is syntactically similar to JavaScript, C++, and Python. So it uses similar language structures to those languages. Strings in Solidity is a data type used to represent/store a set of characters. Examples: "Hii" // Valid string "Hello World" // Valid string "2022" // Valid string In Solidi
3 min read
Solidity - ArraysArrays are data structures that store the fixed collection of elements of the same data types in which each and every element has a specific location called index. Instead of creating numerous individual variables of the same type, we just declare one array of the required size and store the element
6 min read
Solidity - Enums and StructsEnums are the way of creating user-defined data types, it is usually used to provide names for integral constants which makes the contract better for maintenance and reading. Enums restrict the variable with one of a few predefined values, these values of the enumerated list are called enums. Option
3 min read
Solidity - MappingsMapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type. Mappings are mostly used to associate t
4 min read
Solidity - ConversionsSolidity is a programming language that is used to write smart contracts for the Ethereum blockchain. One important concept in Solidity is conversions, which allow you to change the type of a variable or expression. The article focuses on discussing three types of conversions in Solidity. The follow
6 min read
Solidity - Ether UnitsIn the world of Ethereum smart contracts, understanding how Ether (ETH) and its subunits work is crucial. Solidity is the programming language used to write these smart contracts, and it interacts directly with Ether, the cryptocurrency of the Ethereum network. This article focuses on discussing Eth
7 min read
Solidity - Special VariablesThere exist special variables and functions in solidity which exist in the global namespace and are mainly used to provide information about the blockchain or utility functions. They are of two types: 1) Block and Transaction Properties: Block Transaction Properties block.coinbase (address payable)C
3 min read
Solidity - Style GuideSolidity is a computer programming language used to create Ethereum smart contracts. These contracts self-execute. The code and the agreements contained therein are enforced by the blockchain network. Solidity is a high-level language, meaning that it is designed to be human-readable and easy to wri
13 min read
Solidity Functions
Solidity - FunctionsA function is basically a group of code that can be reused anywhere in the program, which generally saves the excessive use of memory and decreases the runtime of the program. Creating a function reduces the need of writing the same code over and over again. With the help of functions, a program can
4 min read
Solidity - Function ModifiersFunction behavior can be changed using function modifiers. Function modifier can be used to automatically check the condition prior to executing the function. These can be created for many different use cases. Function modifier can be executed before or after the function executes its code. The modi
8 min read
Solidity - View and Pure FunctionsThe view functions are read-only function, which ensures that state variables cannot be modified after calling them. If the statements which modify state variables, emitting events, creating other contracts, using selfdestruct method, transferring ethers via calls, Calling a function which is not 'v
2 min read
Solidity - Fall Back FunctionThe solidity fallback function is executed if none of the other functions match the function identifier or no data was provided with the function call. Only one unnamed function can be assigned to a contract and it is executed whenever the contract receives plain Ether without any data. To receive E
3 min read
Solidity Function OverloadingFunction overloading in Solidity lets you specify numerous functions with the same name but varying argument types and numbers.Solidity searches for a function with the same name and parameter types when you call a function with certain parameters. Calls the matching function. Compilation errors occ
1 min read
Mathematical Operations in SoliditySolidity is a brand-new programming language created by the Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 led by Christian Reitwiessner. Ethereum is a decentralized open-source platform based on blockchain domain, used to run smart contrac
6 min read
Solidity Advanced
Solidity - Basics of ContractsSolidity Contracts are like a class in any other object-oriented programming language. They firmly contain data as state variables and functions which can modify these variables. When a function is called on a different instance (contract), the EVM function call happens and the context is switched i
4 min read
Solidity - InheritanceInheritance is one of the most important features of the object-oriented programming language. It is a way of extending the functionality of a program, used to separate the code, reduces the dependency, and increases the re-usability of the existing code. Solidity supports inheritance between smart
6 min read
Solidity - ConstructorsA constructor is a special method in any object-oriented programming language which gets called whenever an object of a class is initialized. It is totally different in case of Solidity, Solidity provides a constructor declaration inside the smart contract and it invokes only once when the contract
4 min read
Solidity - Abstract ContractAbstract contracts are contracts that have at least one function without its implementation or in the case when you don't provide arguments for all of the base contract constructors. Also in the case when we don't intend to create a contract directly we can consider the contract to be abstract. An i
3 min read
Solidity - Basics of InterfaceInterfaces are the same as abstract contracts created by using an interface keyword, also known as a pure abstract contract. Interfaces do not have any definition or any state variables, constructors, or any function with implementation, they only contain function declarations i.e. functions in inte
2 min read
Solidity - LibrariesLibraries in solidity are similar to contracts that contain reusable codes. A library has functions that can be called by other contracts. Deploying a common code by creating a library reduces the gas cost. Functions of the library can be called directly when they do not modify the state variables i
4 min read
Solidity - AssemblyAssembly or Assembler language indicates a low-level programming language that can be converted to machine code by using assembler. Assembly language is tied to either physical or a virtual machine as their implementation is an instruction set, and these instructions tell the CPU to do that fundamen
4 min read
What are Events in Solidity?Solidity Events are the same as events in any other programming language. An event is an inheritable member of the contract, which stores the arguments passed in the transaction logs when emitted. Generally, events are used to inform the calling application about the current state of the contract, w
2 min read
Solidity - Error HandlingSolidity has many functions for error handling. Errors can occur at compile time or runtime. Solidity is compiled to byte code and there a syntax error check happens at compile-time, while runtime errors are difficult to catch and occurs mainly while executing the contracts. Some of the runtime erro
6 min read
Top 50 Solidity Interview Questions and Answers Solidity is an object-oriented programming language used to implement smart contracts on blockchain platforms like Ethereum, which generates transaction records in the system. To excel in your journey toward top companies as a Solidity developer, you need to master some important Solidity Interview
15+ min read