In this post, we will understand the how Input & output Parameters can be defined in Solidity Language.
Advertisements
Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
pragma solidity 0.4.8;
/*
* @title An Example
* @author Toshendra Sharma
* @notice Example for the Solidity Course
*/
// As in Javascript, functions may take parameters as input;
// unlike in Javascript and C, they may also return arbitrary number of parameters as output.
// The input parameters are declared the same way as variables are.
// As an exception, unused parameters can omit the variable name.
contract InputAndOutputParameters {
uint public constructorInput1;
address public constructorInput2;
uint public sum;
uint public product;
uint public multiplyByThreeValue;
// For example, suppose we want our contract to accept
//two parameters, we would write something like:
function InputAndOutputParameters(uint _inputParam1, address _inputParam2) {
// Initialize state variables here
constructorInput1 = _inputParam1;
constructorInput2 = _inputParam2;
}
// The output parameters can be declared with the same syntax after the returns keyword.
// For example, suppose we wished to return one value: then we would write this:
function multiplyByThree(uint _inputParam1) returns (uint m){
multiplyByThreeValue = _inputParam1 * 3;
m = multiplyByThreeValue;
}
// Let's say we want to return two values the we would write something like this
function sumAndProduct1(uint _inputParam1, uint _inputParam2) returns (uint s, uint p){
sum = _inputParam1 + _inputParam2;
product = _inputParam1 * _inputParam2;
s = sum;
p = product;
}
// please note that variables name can be ommitted in returns as well as in function call.
function sumAndProduct2(uint _inputParam1, uint _inputParam2) returns (uint, uint){
sum = _inputParam1 + _inputParam2;
product = _inputParam1 * _inputParam2;
return (sum, product);
}
}