Wasiu-Akindoyin

Posted on

# Data Types and Variables in Solidity

## Introduction

Data types define the kind of data that can be stored in a variable, and Variables are used to store data.

Solidity has a variety of data types and variables, each with its own purpose.

In Solidity, data types play a crucial role in defining the kind of data a variable can hold, allowing you to efficiently manage and manipulate information within your smart contracts.

Understanding the various data types is essential for creating smart contracts.

However, one of the key restrictions in Solidity is that the developer needs to specify the data type of every variable at compile time.

Once declared, the compiler knows exactly how much space it needs to reserve for the variable.

Solidity data types can be broadly categorized into two main groups:

• Value Types
• Reference Types

## Value Types in Solidity

Solidity value types, such as integers, are variables that store data within a defined memory space and pass a duplicated value when used within a function or an assignment. This value type stores a separate copy of the duplicated data type, so any change to the value of the copied value type will not alter the original value type. Here are value types in solidity and what they stand for:

## Signed Integer

A signed integer, declared with the int keyword, is a value data type that can be used to store either positive or negative values in smart contracts. Example of a signed integer in Solidity:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Integer {
int256 sampleInt = 10;
}
``````

## Unsigned Integer

An unsigned integer, declared with the uint keyword, is a value data type that must be non-negative; its value is greater than or equal to zero. Example of an unsigned integer in Solidity:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract UnsignedInteger {
uint256 sampleUint = 20;
}
``````

## Boolean

The bool value data type is used in Solidity to illustrate cases that have binary results. A bool has two fixed values: true or false, with false being the default. Solidity supports the following comparison and logical operators:

• != (Not Equal)
• == (Equal)
• ! (Logical NOT)
• && (Logical AND)
• || (Logical OR)

Example of a boolean in Solidity:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Boolean {
bool sampleBool = false;
}
``````

This is used to store Ethereum addresses. Addresses represent users, contracts, or externally-owned accounts (EOAs). Example of an address in Solidity:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

}
``````

## Enum

Enum (or Enumeration) in Solidity consists of user-defined data types. This data type is used explicitly for constant values, such as the names of integral constants, making a smart contract easier to read and maintain. Example of an enum in Solidity:

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Enum {
// Enum representing different values
enum Status {
EnumValue1,
EnumValue2,
EnumValue3
}
}
``````

## Contracts and Functions

Similar to other object-oriented languages, contract, and function types are used to represent classes and their functions respectively. Contracts contain functions that can modify the contract’s state variables. Here’s an example of contract and function data type in Solidity:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract ValueTypes {
function foo() public view returns (uint) {
return 1;
}

function bar() public view returns (uint) {
uint result = foo();
return result;
}
}
``````

## Reference Types in Solidity

Solidity reference types are variables that point to the memory address of stored data. Unlike value types, reference types do not store any value but serve as a location tracker of the intended data.

Reference types are primarily categorized as follows:

## Strings

Strings in solidity are dynamically-sized arrays of characters used to store and manipulate text data. Below is the Solidity program to implement a string:

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract String {
string firstString = "Hello World!";
}
``````

## Structs

­­
Structs in Solidity are complex data types that allow you to create a custom type containing members of other types, usually used to group linked data. Below is the Solidity program to implement a struct:

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract TodoList {
// Declare a struct which groups together three data types
struct TodoItem {
string text;
bool completed;
uint256 number;
}
}
``````

## Mappings

Mappings store data in key-value pairs similar to dictionaries in other object-oriented languages, with the key being a value data type, and value being any type. Below is the Solidity program to implement a mapping:

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
}
``````

## Variables in Solidity

In Solidity, variables are used to store and manage data within smart contracts. Solidity supports three types of variables which are:

## State Variables

State variables are declared inside the contract. These are variables whose values are permanently stored in a contract storage. Below is a Solidity program to implement the State variable:

``````//SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract StateVariableExample {
//State variable
uint public counter = 0;

function incrementCounter() public {
counter++;
}
}
``````

Local Variables
Local variables are defined inside functions. Function parameters are always local to that function. Below is a Solidity program to implement the Local variable:

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract LocalVariableExample {
function getDouble(uint value) public pure returns (uint) {
// Local variable
uint doubleValue = value * 2;
return doubleValue;
}
}
``````

## Global Variables

Global variables are special variables provided by the Solidity language. They are available throughout the contract and provide information about the blockchain, transaction, and execution context. Examples of Global Variables:

block.timestamp (current block timestamp)

msg.sender (address of the sender of the current function call)

msg.value (amount of ether sent in the current function call)

## Naming Variables in Solidity

While naming your variables in Solidity, keep the following rules in mind:

• You should not use any of the Solidity reserved keywords as a variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.
• Solidity variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one.
• Solidity variable names are case-sensitive. For example, Name and name are two different variables.

## Declaration of Variables

In Solidity declaration of variables is a little bit different, to declare a variable the user has to specify the data type first followed by access modifier.

Syntax:

``````<Data type> <access modifier> <variable name>;

``````

Example:

``````uint256 public myNumber;

``````

## Conclusion

Enjoy your Solidity development journey!

In this article, we have learned a lot! You now know the different data types in solidity, variables, naming variables, and declaration of variables in solidity.
It’s a wrap!

Thank you for sticking around till the end. Let's connect on LinkedIn and X.