Open In App

Keywords in Solidity

Solidity keywords are words that are specifically reserved for their intended usage in the Solidity programming language and cannot be used elsewhere, such as variable or function names. To create smart contracts on Ethereum and other blockchain platforms, these terms are fundamental building blocks. pragma, contract, function, modifier, event, require, and mapping are a few instances of Solidity keywords.

Correct Solidity keyword usage is essential for creating effective smart contracts. A keyword misused or misspelled might cause syntax mistakes or unexpected contract behavior. To create high-quality and secure contracts, Solidity developers need to become familiar with the terminology and its semantics. A complete list of all the keywords and their applications may be found in the Solidity documentation.



Here are some examples of reserved keywords in Solidity:

pragma abstract address bool break bytes enum
function if mapping modifier return struct this
while assembly constant event external fallback internal
payable public new pure require revert super

pragma: Used to specify the version of the Solidity compiler that the contract should be compiled with. 



pragma solidity ^0.8.0;

abstract: This declares an abstract contract MyContract with a function myFunction that has no implementation.

abstract contract MyContract {

   function myFunction() public virtual returns (uint256);

}

address: This declares a public variable myAddress of type address.

address public myAddress;

bool: This declares a public variable isComplete of type bool with an initial value of false.

bool public isComplete = false;

break: This uses the break keyword to exit the for loop when the condition is met.

for (uint i = 0; i < myArray.length; i++) {

   if (myArray[i] == value) {

      break;

   }

}

bytes: This declares a public variable myBytes of type bytes with an initial value of 0x001122.

bytes public myBytes = hex”001122″;

enum: This declares an enum type MyEnum with three possible values and a public variable myEnum of type MyEnum with an initial value of Value1.

enum MyEnum {Value1, Value2, Value3}

MyEnum public myEnum = MyEnum.Value1;

function: This declares a function myFunction with a single parameter myParam.:

function myFunction(uint256 myParam) public {

  // function code here

}

if: This uses the if keyword to conditionally execute code.

if (myValue > 10) {

  // code to execute if myValue is greater than 10

}

mapping: This declares a public variable myMapping of type mapping that maps Ethereum addresses to unsigned integers.
 

mapping (address => uint256) public myMapping;

modifier: This declares a modifier onlyOwner that adds a check to the function to ensure that only the contract owner can call it.

modifier onlyOwner() {

  require(msg.sender == owner, “Only owner can call this function.”);

  _;

}

return: This declares a function myFunction that returns an unsigned integer.

function myFunction() public returns (uint256) {

  uint256 myValue = 42;

  return myValue;

}

struct: This declares a struct type MyStruct with two members, and a public variable myStruct of type MyStruct with an initial value of MyStruct(10, true).
 

struct MyStruct {

  uint256 value1;

  bool value2;

}

MyStruct public myStruct = MyStruct(10, true);

this: This uses this keyword to refer to the contract itself.
 

function myFunction() public {

  require(msg.sender == address(this), “Function can only be called by the contract itself.”);

}

while: This uses the while keyword to loop while the condition is true.
 

uint256 i = 0;

while (i < 10) {

  i++;

}

assembly: This declares an assembly block, which allows low-level, untyped access to the Ethereum Virtual Machine (EVM).

assembly {

  // assembly code here

}

constant: This declares a function myFunction that returns a constant value.

function myFunction() public constant returns (uint256) {

  return 42;

}

event: This declares an event that can be emitted by the contract to notify external parties of an occurrence.

event MyEvent(address indexed _from, uint256 _value);

external: This declares a function that can only be called externally, i.e. from outside the contract.

function myFunction() external {

  // function code here

}

fallback: This declares the fallback function, which is executed when a transaction is sent to the contract with no function specified.

fallback() external payable {

  // fallback function code here

}

internal: This declares a function that can only be called from within the contract or its derived contracts.

function myFunction() internal {

  // function code here

}

new: This uses the new keyword to create a new instance of the MyContract contract.

MyContract myContract = new MyContract();

payable: This declares a function that can receive Ether as part of the transaction.

function myFunction() payable {

  // function code here

}

private: This declares a private variable myPrivateValue that can only be accessed from within the contract.

uint256 private myPrivateValue;

public: This declares a public variable myPublicValue that can be accessed from outside the contract.

uint256 public myPublicValue;

pure: This declares a function that does not access or modifies the contract’s state, i.e. it is a pure function.

function myFunction(uint256 _value) public pure returns (uint256) {

  return _value * 2;

}

require: This uses the require keyword to check a condition and revert the transaction if it is not met.

function myFunction(uint256 _value) public {

  require(_value > 0, “Value must be greater than 0.”);

  // function code here

}

revert: This uses the revert keyword to revert the transaction with an error message.

function myFunction() public {

  revert(“Function is not implemented yet.”);

}

super: This uses the super keyword to call the parent contract’s myFunction function.

function myFunction() public {

  super.myFunction();

  // function code here

}

Permission Keywords

Four permission keywords can be used in Solidity to restrict access to certain functions:

Modifier Keywords

A modifier in Solidity is a unique kind of function that can be used to change how other functions behave. The modifier keyword is used to declare modifiers, which are then followed by a name and a code block. When a function declaration includes a modifier, the modifier code is performed first. These are a few Solidity modifier words:


Article Tags :