Merge pull request #5323 from ethereum/docs-input-output-change-control-struct

Change terminology used for input output parameters
This commit is contained in:
chriseth 2018-11-14 15:56:02 +01:00 committed by GitHub
commit 88fbd315bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,23 +2,22 @@
Expressions and Control Structures Expressions and Control Structures
################################## ##################################
.. index:: ! parameter, parameter;input, parameter;output, parameter;multiple .. index:: ! parameter, parameter;input, parameter;output, function parameter, parameter;function, return variable, variable;return, return
Input Parameters and Output Parameters Function Parameters and Return Variables
====================================== ========================================
As in Javascript, functions may take parameters as input; As in JavaScript, functions may take parameters as input. Unlike in JavaScript
unlike in Javascript and C, they may also return arbitrary number of and C, functions may also return an arbitrary number of values as output.
parameters as output.
Input Parameters Function Parameters
---------------- -------------------
The input parameters are declared the same way as variables are. Function parameters are declared the same way as variables, and the name of
The name of unused parameters can be omitted. unused parameters can be omitted.
For example, suppose we want our contract to
accept one kind of external calls with two integers, we would write For example, if you want your contract to accept one kind of external call
something like:: with two integers, you would use something like::
pragma solidity >=0.4.16 <0.6.0; pragma solidity >=0.4.16 <0.6.0;
@ -29,18 +28,18 @@ something like::
} }
} }
Input parameters can be used just as any other local variable Function parameters can be used as any other local variable and they can also be assigned to.
can be used, they can also be assigned to.
.. index:: return array, return string, array, string, array of strings, dynamic array, variably sized array, return struct, struct .. index:: return array, return string, array, string, array of strings, dynamic array, variably sized array, return struct, struct
Output Parameters Return Variables
----------------- ----------------
The output parameters can be declared with the same syntax after the Function return variables are declared with the same syntax after the
``returns`` keyword. For example, suppose we wished to return two results: ``returns`` keyword.
the sum and the product of the two given integers, then we would
write:: For example, suppose you want to return two results: the sum and the product of
two integers passed as function parameters, then you use something like::
pragma solidity >=0.4.16 <0.6.0; pragma solidity >=0.4.16 <0.6.0;
@ -55,12 +54,31 @@ write::
} }
} }
The names of output parameters can be omitted. The names of return variables can be omitted.
The return values can be specified using ``return`` statements, Return variables can be used as any other local variable and they
which are also capable of :ref:`returning multiple values<multi-return>`. are zero-initialized. If they are not explicitly
Return parameters can be used as any other local variable and they set, they stay zero value.
are zero-initialized; if they are not explicitly
set, they stay zero. You can either explicitly assign to return variables and
then leave the function using ``return;``,
or you can provide return values
(either a single or :ref:`multiple ones<multi-return>`) directly with the ``return``
statement::
pragma solidity >=0.4.16 <0.6.0;
contract Simple {
function arithmetic(uint _a, uint _b)
public
pure
returns (uint o_sum, uint o_product)
{
return (_a + _b, _a * _b);
}
}
This form is equivalent to first assigning values to the
return variables and then using ``return;`` to leave the function.
.. note:: .. note::
@ -90,12 +108,12 @@ Solidity.
.. _multi-return: .. _multi-return:
Returning Multiple Values Returning Multiple Variables
------------------------- ----------------------------
When a function has multiple output parameters, ``return (v0, v1, ..., When a function has multiple return types, ``return (v0, v1, ...,
vn)`` can return multiple values. The number of components must be vn)`` can return multiple values. The number of components must be
the same as the number of output parameters. the same as the number of return types.
.. index:: ! function;call, function;internal, function;external .. index:: ! function;call, function;internal, function;external