Solidity

Solidity Input and Output Parameters

Solidity Input and Output Parameters Main Tips

  • In Solidity, much like in JavaScript, functions can take input in the form of parameters.
  • An arbitrary number of the parameters may also be returned as output.

Solidity Input and Output Parameters Input

The parameters used for input get declared similarly to normal variables.In the form of an exception, parameters that are not used may omit the variable name. For example, let’s say we want this contract to accept one sort of external calls using two integers. For that, we would write something like this:

Example

pragma solidity ^0.4.0;

contract SimpleContract {
    function taker(uint _x, uint _y) {
        // execute code using _x and _y.
    }
}

 

Try on Remix Try live on Hosting  


Solidity Input and Output Parameters Output

The parameters that are used for output may be declared using the same syntax after the keyword returns. For example, let’s say we would like to return two results: the product of the given integers and the sum. This is the code we could use to accomplish this task:

Example

pragma solidity ^0.4.0;

contract SimpleContract {
    function arithmetics(uint _x, uint _y) returns (uint o_product, uint o_sum) {
        o_sum = _x + _y;
        o_product = _x * _y;
    }
}

 

Try on Remix Try live on Hosting

The output parameter names may be omitted. The values used for output can also be specified with return statements. The return statements can also return more than one value. Return parameters get initialized to zero; in case they are not set explicitly, they stay at zero.

Input and output parameters may be used in the form of expressions in the body of the function. There, they can also be used on the assignment’s left-hand side.

Read previous post:
Solidity Units And Global Variables

Solidity Units And Global Variables Main Tips Solidity provides two types of units - Ether units and time units. You...

Close