Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.62 KB

File metadata and controls

62 lines (45 loc) · 1.62 KB

30 Days Of Solidity: Functions

Twitter Follow

Author: Vedant Chainani
June, 2022

<< Day 6 | Day 8 >>

Day 7


📔 Day 7

Functions

A 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.

Syntax -

function function_name(parameter_list) scope returns(return_type) {
       // block of code
}

eg-

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract MyContract {
    function add() public view returns (uint256) {
        uint256 num1 = 10;
        uint256 num2 = 16;
        uint256 sum = num1 + num2;
        return sum;
    }

    function sqrt(uint256 num) public pure returns (uint256) {
        num = num**2;
        return num;
    }
}

view in a function ensures that they will not modify the state of the function.


<< Day 6 | Day 8 >>