Variables, Data Types, and Functions in Solidity

In this lesson, you'll learn the fundamental building blocks of smart contracts: variables, data types, and functions in Solidity. You'll gain the essential knowledge needed to store, manipulate, and process data within your smart contracts. By the end, you'll be able to create simple contracts that perform basic operations.

Learning Objectives

  • Define and declare variables of various data types in Solidity.
  • Understand the different data types available in Solidity and when to use them.
  • Write and call functions within a Solidity smart contract.
  • Explain the difference between local and state variables.

Lesson Content

Variables: The Data Holders

Variables are like containers that hold data. In Solidity, you declare a variable by specifying its data type and giving it a name. There are two main types of variables: state variables and local variables. State variables are stored on the blockchain and are part of the contract's state, while local variables exist only within a function's scope.

Example:

pragma solidity ^0.8.0;

contract SimpleStorage {
    // State variable of type uint (unsigned integer)
    uint public storedData;

    // Function to set the value of storedData
    function set(uint x) public {
        storedData = x;
    }

    // Function to get the value of storedData
    function get() public view returns (uint) {
        return storedData;
    }
}

Quick Check: Which data type is used to store whole numbers?

Data Types: The Data Classifiers

Solidity supports various data types to represent different kinds of data. Understanding these is crucial.

  • uint: Unsigned integer (e.g., uint8, uint16, uint256 - common for gas efficiency)
  • int: Signed integer
  • bool: Boolean (true or false)
  • address: Ethereum address (e.g., an account or contract address)
  • string: Text
  • bytes: Raw binary data
  • Arrays: Lists of elements of the same type (e.g., uint[] creates an array of unsigned integers)

Example:

pragma solidity ^0.8.0;

contract DataTypes {
    uint public myUint = 10;
    bool public myBool = true;
    address public myAddress = 0x5B38DA6a701c5eF7100bF576627C1E6eA2722137; // Example address
    string public myString = "Hello, World!";
    uint[] public myArray = [1, 2, 3, 4, 5];
}

Quick Check: What keyword is used to declare a variable that can be accessed from any contract?

Functions: The Action Performers

Functions are blocks of code that perform specific tasks. They are the heart of your smart contract logic. Functions can take input parameters, process data, and return output values. Functions can be declared with different visibility levels (public, private, internal, external). public functions can be called from anywhere, private only from within the contract, internal also from derived contracts and external can only be called from outside the contract.

Example:

pragma solidity ^0.8.0;

contract SimpleFunction {
    // Public function that adds two numbers
    function add(uint a, uint b) public pure returns (uint) {
        return a + b;
    }

    // Private function (only callable from within the contract)
    function _multiply(uint a, uint b) private pure returns (uint) {
        return a * b;
    }
}

Note: The pure keyword means the function doesn't read or modify the contract's state. view means the function reads, but doesn't modify the state.

Quick Check: Which of the following is NOT a valid Solidity data type?

Progress
0%