▫️Lesson 2.3: Functions in Solidity

Module 2: Developing Smart Contracts with Solidity

Objective

This lesson aims to provide a comprehensive understanding of functions in Solidity, including their declaration, types, visibility, and modifiers. Functions are the primary way to execute logic and manipulate contract data in Solidity, making them a crucial aspect of smart contract development.


Function Declaration

A function in Solidity is declared using the function keyword, followed by its name, parameters, visibility, modifiers, and return types.

Syntax:

function functionName(parameters) visibility modifier returns (returnTypes) {
    // function body
}

Example:

pragma solidity ^0.8.0;

contract FunctionExample {
    uint public value;

    // Function to set the value
    function setValue(uint _value) public {
        value = _value;
    }

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

Function Types

  • View Functions: Promise not to modify the state.

  • Pure Functions: Promise not to read from or modify the state.

  • Payable Functions: Can receive Ether.

Example:

pragma solidity ^0.8.0;

contract FunctionTypesExample {
    uint public result;

    // View function
    function viewFunction() public view returns (uint) {
        return result;
    }

    // Pure function
    function pureFunction(uint a, uint b) public pure returns (uint) {
        return a + b;
    }
}

Visibility

  • Public: Accessible from within the contract, by derived contracts, and externally.

  • External: Only accessible from external calls.

  • Internal: Accessible within the contract and in derived contracts.

  • Private: Only accessible within the contract.

Example:

pragma solidity ^0.8.0;

contract VisibilityExample {
    // Public function
    function publicFunction() public pure returns (string memory) {
        return "Public Function";
    }

    // External function
    function externalFunction() external pure returns (string memory) {
        return "External Function";
    }

    // Internal function
    function internalFunction() internal pure returns (string memory) {
        return "Internal Function";
    }

    // Private function
    function privateFunction() private pure returns (string memory) {
        return "Private Function";
    }
}

Modifiers

Modifiers are used to change the behavior of functions in Solidity. They can be used to restrict access, validate inputs, or guard against reentrancy attacks.

Example:

pragma solidity ^0.8.0;

contract ModifierExample {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Not the owner");
        _;
    }

    function restrictedFunction() public onlyOwner {
        // Only the owner can call this function
    }
}

Activity: Creating and Testing Functions

  1. Implement a Contract: Using Remix IDE, create a contract that includes functions of various types, visibilities, and modifiers.

  2. Test Each Function: Deploy your contract and test each function to understand how different types, visibilities, and modifiers affect function behavior and access.


Exercise

Hands-On Practice:

  • Extend the FunctionExample contract by adding a modifier that checks if the input value is greater than a certain threshold before setting the value. Implement this modifier in the setValue function.


This lesson has covered the fundamentals of functions in Solidity, including their declaration, types, visibility, and modifiers. Understanding these concepts is essential for developing sophisticated and secure smart contracts on the Ethereum blockchain.

Last updated