Open In App
Related Articles

Solidity – Functions

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. With the help of functions, a program can be divided into many small pieces of code for better understanding and managing.

Declaring a Function

In Solidity a function is generally defined by using the function keyword, followed by the name of the function which is unique and does not match with any of the reserved keywords. A function can also have a list of parameters containing the name and data type of the parameter. The return value of a function is optional but in solidity, the return type of the function is defined at the time of declaration.

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

Solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate function declaration
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to calculate the sum of two numbers
contract Test {
    // Defining a function to calculate sum of two numbers
    function add() public pure returns(uint){
        uint num1 = 10;
        uint num2 = 16;
        uint sum = num1 + num2;
        return sum;
    }
}

                    

Output :

 Declaring a Function 

In the above example, you must have seen the usage of “pure”. pure in a function ensures that they will not modify the state of the function. The following statements if present in the function will modify the state of the function and the compiler will throw a warning.

  • Modifying state variables.
  • Emitting events.
  • Creating other contracts.
  • Using self-destruct.
  • Sending Ether via calls.
  • Calling any function which is not marked pure or view.
  • Using inline assembly containing certain opcodes.
  • Using low-level calls.

Function Calling

A function is called when the user wants to execute that function. In Solidity the function is simply invoked by writing the name of the function where it has to be called. Different parameters can be passed to function while calling, multiple parameters can be passed to a function by separating with a comma. 

Solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate function calling
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to call one function into the another function
contract Test {
    //Defining public pure sqrt function
    function sqrt(uint _num) public pure returns(uint){
       _num = _num ** 2;
       return _num;
    }
    // Defining a public pure function to demonstrate
    // calling of sqrt function
    function add() public pure returns(uint){
      uint num1 = 10;
      uint num2 = 16;
      uint sum = num1 + num2;
      return sqrt(sum); // calling function
   }
}

                    

Output :

 Function Calling

Return Statements

A return statement is an optional statement provided by solidity. It is the last statement of the function, used to return the values from the functions. In Solidity, more than one value can be returned from a function. To return values from the function, the data types of the return values should be defined at function declaration. 

Solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate return statements
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to return a value from the function
contract Test {
    // Defining a public pure function to
    // demonstrate return statement
    function return_example() public pure returns(uint, uint, uint, string memory){
      uint num1 = 10;
      uint num2 = 16;
      uint sum = num1 + num2;
      uint prod = num1 * num2;
      uint diff = num2 - num1;
      string memory message = "Multiple return values";
      return (sum, prod, diff, message);
   }
}

                    

Output :

 Return Statement



Last Updated : 24 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads