Ethereum Solidity Function Parameters


Solidity functions, like any other programming language, take input parameters and return output parameters. They can also return arbitrary number of parameters as output (this is similar to Tuples in C# 7). 

Input Parameters

The input parameters are defined same as variables. A function can take multiple number of input parameters. In our service if we want to hire a contractor we specify the contract address as the input parameter.
/// @notice hiring the contractor to perform the work
/// @param _contractor ethereum address of the contractor
function hire (address _contractor) onlyClient {
    contractor = _contractor;
}

Output Parameters

Solidity functions support multiple ways of returning parameters. The function return type should be declared with returns keyword. In our service if we want to return the contractor address, then we would write as below:
/// @notice Get the contractor assigned to the service
/// @return contractor address
function getContractorAddress() public constant returns (address) {
    return contractor;
}

Similar to other languages we can use the keyword return to return the function output. Solidity supports named parameters we can provide name of the return parameter as shown below:
/// @notice Get the contractor assigned to the service
/// @return contractor address
function getContractorAddress() public constant returns (address _contractor) {
    _contractor = contractor;
}

In the above code we provided variable name to the return parameter, _contractor. Assigning value to the variable will automatically return its value.

Solidity supports returning values. It internally uses tuple types, i.e. a list of objects of potentially different types whose size is a constant at compile-time. Those tuples can be used to return multiple values at the same time. The functionality is similar to tuples in C# 7. Let’s return all the addresses in our Service Contract
/// @notice Get the addresses of client, contractor and 0x platform
/// @return client, contractor and platform address
function getAllAddresses() public constant returns (address, address, address) {
    return (client, contractor, platform);
}


1 comment:

Ethereum Solidity Compiler

Ethereum contracts are written in the high level language called Solidity and compiled into bytecode using the Solidity compiler. This ...