In this paper, the main content comes from: https://solidity-cn.readthedocs.io/zh/latest/
Five, expression and control structure
5.1 input parameters and output
Like Javascript, functions may require parameters as input; With Javascript and C, they may return to any number of parameters as output,
The input parameter
Input parameters of declarative way the same as the variables, but there is one exception, unused parameters can be omitted, for example, if we want the contract to accept function has two integer parameters of external calls, we would like this to write
Pragma solidity ^ 0.4.16;
Contract Simple {
The function taker (uint _a, uint _b) public pure {
//by _a and _b implementation of related functions.
}
}
Output parameter
Output parameters of declarative way after keywords returns, and the input parameters declared in the same way, for example, if we need to return two results: the two given integers, and with the product, we should write
Pragma solidity ^ 0.4.16;
Contract Simple {
The function arithmetics (uint _a, uint _b)
Public
Pure
Returns (uint o_sum, uint o_product)
{
O_sum=_a + _b;
O_product=_a * _b;
}
}
Output parameter name can be omitted, the output value can also use the return statement specified, return statements can also return more value, refer to: ref: multi - return, return the output parameters are initialized to 0; If they were not explicit assignment, they would have been to 0,
Input and output parameters can be used as the expression in the function body, therefore, they can also be used to the left of the equals sign is assigned,
5.2 control structure
Most of the JavaScript control structure is available in the Solidity, in addition to the switch and goto, so there is an if inside a Solidity, else, while, do, for, break, continue, return to? : the same as expressed in C or JavaScript semantic keywords,
Used to represent the condition of the brackets can not be omitted, single statement the braces on both sides of the body can be omitted,
Note that unlike C and JavaScript, Solidity china-africa Boolean type value cannot be converted to Boolean type, so the if (1) {... } notation is invalid in the Solidity,
5.3 assignment
Deconstruction assignment and return value more
Solidity internal allows the tuples (a tuple) type, as well as a number elements fixed at compile time list of objects, the elements in the list can be different types of objects, these tuples can be used to return multiple values at the same time, they can also be used to simultaneously to multiple new statement variables or existing (or usually LValues) :
Pragma solidity & gt; 0.4.23 & lt; 0.5.0;
Contract C {
Uint [] data;
The function f () public pure returns (uint, bool, uint) {
Return (7, true, 2);
}
The function g () public {
//based on the returned tuples to declare variables and assignment
(uint x, bool b, uint y)=f ();
//the exchange of two values general tips - but not for the value types of storage (storage) variables,
(x, y)=(y, x);
//the end of the tuple element can be omitted (this also applies to variable declarations),
(the data length,,)=f ();//set the length of 7
//the ellipsis at the end of tuple element method and only can be used in the top left of the assignment operation, in addition to the exception:
(x)=(1);
//(1) is the only way to specify the singleton tuple, because (1)
//equivalent to 1,
}
}
Until 0.4.24 version, to have fewer tuples of yuan prime assignment can be possible, in both the left and right side (on the last empty out several elements, for example), now, it's not recommended, on both sides of an assignment operation should have the same number of elements,
5.4 scope and statement
After the variable declarations will have the default initial value, its initial value bytes are all zero, any type of variable "default" is a typical of the type of its corresponding "zero state", for example, a bool type of the default value is false, uint or type int the default value is 0, arrays, and for the size of the static bytes1 bytes32, each individual elements will be initialized to default values of the corresponding to its type, and finally, for the size of the dynamic array of bytes and type string, the default default value is an empty array or string,
Solidity of scoping rules followed C99 (and many other languages) : variables will be declared after the visible from them, until the end of a pair of {} block as an exception, in a for loop statement initialize variables, their visibility only keep to the end of the for loop,
Beyond those defined in the code block variables, such as function, contracts, custom types, etc., will not affect their scope characteristics, this means that you can state variables in the actual statement statement before using them, and recursively call function,
Based on the above rules, the example below won't appear compiler warnings, because the two variables although the name is same, but in different scopes,
Pragma solidity & gt; 0.4.24;
Contract C {
The function minimalScoping () pure public {
{
Uint same2=0;
}
{
Uint same2=0;
}
}
}
As special case of C99 scope rules, please note that in the example below, the first value assigned to x will change on a layer of declared in the values of the variables, if the outer statement variables are "shadow" (that is, be inside the scope by a variable with the same replaced) you will get a warning,
Pragma solidity & gt; 0.4.24;
Contract C {
The function f () pure public returns (uint) {
Uint x=1;
{
X=2;//this assignment will affect variables in outer statement
Uint x;
}
return x;//x has the value 2
}
}
In Solidity 0.5.0 previous version, use the Javascript rules, scope of the rules that you can declare a variable in the function of any position, can make him within the scope of the entire function, and this rule will be broken, since version 0.5.0 starting 0.5.0 version, in the following example code will cause a compiler error,
//it will not be able to compile
Pragma solidity & gt; 0.4.24;
Contract C {
The function f () pure public returns (uint) {
X=2;
Uint x;
return x;
}
}
5.5 error handling: Assert, the Require, Revert and Exceptions
Solidity using state recovery exception to handle errors, this exception will cancel the current call (and all its child call) the state of all changes, and also to the caller markup errors,
Convenience function asserts and the require can be used to check the conditions and the conditions are not met when an exception is thrown, assert function can only be used for testing internal error, and examine the variables, the require function is used to confirm the validity conditions, such as the input variables, or state variables whether meet the contract conditions, or verify the external call returns the value of the contract,
When used properly, analysis tool to assess your contract, and mark out those will make assert failure conditions and function call, the normal work of the code does not lead to a failure of assert statements; If this happens, then there was a need to fix the bug you,
There are two other ways to trigger the exception: revert function can be used to mark the error and restore the current call, revert call contains detailed information about the error is possible, the message will be returned to the caller, are not recommended throw keyword can also be used to replace revert () (but cannot return error messages),
Since 0.4.13 version, throw the key word is abandoned, and the future will be phased out,
When the call when an exception occurs, they will automatically "bubble" (that is, to throw an exception), exception to this rule is the send and low-level function call, delegatecall and callcode - if an exception occurs, the function returns false, rather than "bubble",
As part of the design of EVM entry, if the contract is called account does not exist, the low-level function call, delegatecall and callcode returns success, so if you need to use low-level function, must be checked before the call is called contract exists,
Exception handling is unrealized
In case, you can see how easily use ` ` require ` ` check input conditions and how to use ` ` assert ` ` check internal error, note that you can provide the require a message string, but not assert,
Pragma solidity ^ 0.4.22;
Contract Sharer {
The function sendHalf (address addr) public payable returns (uint balance) {
Require (MSG. Value % 2==0, "Even the value of required.");
Uint balanceBeforeTransfer=this. The balance;
Addr. Transfer (MSG. The value/2);
//as a result of transfer function in failure when an exception is thrown and cannot be back here, so we should have no way to still have half of the money,
Assert (enclosing the balance==balanceBeforeTransfer - MSG. The value/2);
Return this. The balance;
}
}
The following situations will produce an assert type exception:
If you visit an array index is too big or negative (such as x [I] I & gt;=x.l ength or I & lt; 0),
If you visit fixed length bytesN index is too big or negative,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull