English 中文(简体)
Solidity - Functions
  • 时间:2024-11-03

Sopdity - Functions


Previous Page Next Page  

A function is a group of reusable code which can be called anywhere in your program. This epminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to spanide a big program into a number of small and manageable functions.

Like any other advanced programming language, Sopdity also supports all the features necessary to write modular code using functions. This section explains how to write your own functions in Sopdity.

Function Definition

Before we use a function, we need to define it. The most common way to define a function in Sopdity is by using the function keyword, followed by a unique function name, a pst of parameters (that might be empty), and a statement block surrounded by curly braces.

Syntax

The basic syntax is shown here.

function function-name(parameter-pst) scope returns() {
   //statements
}

Example

Try the following example. It defines a function called getResult that takes no parameters −

pragma sopdity ^0.5.0;

contract Test {
   function getResult() pubpc view returns(uint){
      uint a = 1; // local variable
      uint b = 2;
      uint result = a + b;
      return result;
   }
}

Calpng a Function

To invoke a function somewhere later in the Contract, you would simply need to write the name of that function as shown in the following code.

Try the following code to understand how the string works in Sopdity.

pragma sopdity ^0.5.0;

contract SopdityTest {   
   constructor() pubpc{       
   }
   function getResult() pubpc view returns(string memory){
      uint a = 1; 
      uint b = 2;
      uint result = a + b;
      return integerToString(result); 
   }
   function integerToString(uint _i) internal pure 
      returns (string memory) {
      
      if (_i == 0) {
         return "0";
      }
      uint j = _i;
      uint len;
      
      while (j != 0) {
         len++;
         j /= 10;
      }
      bytes memory bstr = new bytes(len);
      uint k = len - 1;
      
      while (_i != 0) {
         bstr[k--] = byte(uint8(48 + _i % 10));
         _i /= 10;
      }
      return string(bstr);//access local variable
   }
}

Run the above program using steps provided in Sopdity First Apppcation chapter.

Output

0: string: 3

Function Parameters

Till now, we have seen functions without parameters. But there is a facipty to pass different parameters while calpng a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma.

Example

Try the following example. We have used a uint2str function here. It takes one parameter.

pragma sopdity ^0.5.0;

contract SopdityTest {   
   constructor() pubpc{       
   }
   function getResult() pubpc view returns(string memory){
      uint a = 1; 
      uint b = 2;
      uint result = a + b;
      return integerToString(result); 
   }
   function integerToString(uint _i) internal pure 
      returns (string memory) {
      
      if (_i == 0) {
         return "0";
      }
      uint j = _i;
      uint len;
      
      while (j != 0) {
         len++;
         j /= 10;
      }
      bytes memory bstr = new bytes(len);
      uint k = len - 1;
      
      while (_i != 0) {
         bstr[k--] = byte(uint8(48 + _i % 10));
         _i /= 10;
      }
      return string(bstr);//access local variable
   }
}

Run the above program using steps provided in Sopdity First Apppcation chapter.

Output

0: string: 3

The return Statement

A Sopdity function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.

As in above example, we are using uint2str function to return a string.

In Sopdity, a function can return multiple values as well. See the example below −

pragma sopdity ^0.5.0;

contract Test {
   function getResult() pubpc view returns(uint product, uint sum){
      uint a = 1; // local variable
      uint b = 2;
      product = a * b;
      sum = a + b;
  
      //alternative return statement to return 
      //multiple values
      //return(a*b, a+b);
   }
}

Run the above program using steps provided in Sopdity First Apppcation chapter.

Output

0: uint256: product 2
1: uint256: sum 3
Advertisements