Assign Value (Assignment)
deconstruct assignment and return multiple results (destructing Assignments and returning multip values)
The solidity built-in support tuple (tuple), that is, supports a list of possible completely different types, fixed in number (tuple generally refers to two, and a triple generally refers to three).
This built-in structure can return multiple results at the same time, and can also be used to assign values to multiple variables at once.
pragma solidity ^0.4.0;
Contract C {uint[] data;
function f () returns (UINT, bool, uint) {return (7, True, 2); } function g () {//declares and assigns the variables.
Specifying the type explicitly is not possible.
var (x, b, y) = f ();
Assigns to a pre-existing variable.
(x, y) = (2, 7);
Common Trick to swap values – does not work for non-value storage.
(x, y) = (y, x);
Components can is left out (also for variable declarations).
If the tuple ends in a empty component,//the rest of the values are discarded. (data.length,) = f ();
Sets the length to 7//the same can is done in the left side. (, data[3]) = f (); Sets Data[3] to 2//components can only being left in the left-hand-side of assignments, with/one
Exception: (x,) = (1,);
(1,) is the only way to specify a 1-component tuple, because (1) is Equivalent to 1.
}
}
complexity of arrays and custom structures (complication for Arrays and Struts)
For non value types, such as arrays and array, the syntax for assigning values is somewhat complicated. assigning to a state variable always creates a completely unrelated copy. Assign to a local variable, creating a completely unrelated copy only for the base type, such as those within 32 bytes of static type (static types). If it is a data structure or array (including bytes and string) types, the state variable is assigned to a local variable, and the local variable simply holds a reference to the original state variable. The local variable is again assigned, and the state variable is not modified, only the reference is modified. However, modifying the member values of this local reference variable changes the value of the state variable.
the order in which expressions are executed (orders of evaluation of Expressions)
The order in which expressions are evaluated is not certain, and more formally, the order in which a node of a node of the expression tree is evaluated is indeterminate, but it is definitely going to be executed before the node itself. We only guarantee that the statement (statements) is executed sequentially and the Boolean expression's short-circuit operation is supported. View the order in which operators are executed. Learn more.
Create contract instance (creating contracts via ' new ')
A contract can create a contract by using the New keyword. The complete code to create a contract must be known in advance, so it is not possible to create dependencies recursively.
pragma solidity ^0.4.0;
Contract account{
UINT Accid;
Construction?
function account (UINT AccountId) payable{
accid = AccountId;
}
}
Contract initialize{account Account
= new account (a);
function NewAccount (UINT AccountId) {Account
= new account (AccountId);
}
function Newaccountwithether (UINT accountid, uint amount) {Account
= (new account). Value (amount) (AccountId);
}
}
As can be seen from the example above, you can send ether in the contract of creation, but you cannot limit the use of gas. If created because of out-of-stack, or if there are not enough balances and any other issues, an exception is thrown.
functions Call (function Calls) internal function call (Internal function Calls)
In the current contract, the function can be called directly (internal invocation method), including also recursive calls, to see a simple example:
Contract C {
function g (UINT a) returns (UINT ret) {return F ();}
function f () returns (UINT ret) {return G (7) + f ();}
}
These function calls are translated into simple jump instructions in EVM. One advantage of this is that the current memory is not recycled. So it's very efficient to pass a memory reference in an internal call. Of course, only the functions of the same contract can be invoked in an internal way. external function call (External function Calls)
The expression THIS.G (8); and C.G (2) (where C is a contract instance) is the way of calling the function externally. Implementation is through a message invocation, not directly through the EVM instruction jump. Note that you cannot use this to invoke a function in the Contract builder because the current contract has not yet been created.
Functions of other contracts must be invoked externally. For an external invocation, the parameters of all functions must be copied into memory.
When you invoke a function of another contract, you can specify the amount of ether (in Wei) and the gas value to be sent, respectively, with the option. Value () and. Gas ().
pragma solidity ^0.4.0;
Contract Infofeed {
function info () payable returns (UINT ret) {return
msg.value;
}
}
Contract Consumer {
function deposit () payable returns (UINT) {return
msg.value;
}
function left () constant returns (UINT) {return
this.balance;
}
function Callfeed (addr) returns (UINT) {return
infofeed (addr). Info.value (1). Gas (8000) ();
}
}
In the code above, we first call deposit () to deposit a certain amount of ether for the consumer contract. The Callfeed () is then invoked to send 1ether to the info () function of the infofeed contract by means of value (1). Note that, if you do not recharge first, because the balance of the contract is 0, the balance is not enough will error invalid Opcode1.
Infofeed.info () function, you must use the payable keyword, otherwise you cannot receive ether through the value () option.
Code infofeed (addr) makes a display of type conversions, stating that we know that the given address is a infofeed type. So the constructor initialization is not performed here. The type of cast that is displayed requires extreme caution and do not attempt to invoke a contract that you do not know the type of.
We can also use function setfeed (infofeed _feed) {feed = _feed;} To directly assign the value. Info.value (1). Gas (8000) is only the local setting of the amount sent and the gas value, and the actual execution of the call is the following bracket. Info.value (1). Gas (8000) ().
If the invoked contract does not exist, or if it is an account that does not package code, or if the calling contract produces an exception, or if there is not enough gas, the function call is abnormal.
If the invoked contract source is not known beforehand, there is a potential risk of interacting with them. The current contract will give its control to the called contract, and the other party can do almost anything. Even if the called contract is inherited from a known parent contract, the inherited child contract is only required to implement the interface correctly. The implementation of the contract, can be arbitrary content, there will be risks. Also, be prepared to process other contracts that call your own system, and you may return the calling contract before the first call result returns. In a way, it means that the invoked contract can change the status variable (state variable) of the calling contract to mark the current state. For example, to write a function, call an external function only if the value of the state variables has a corresponding change, so that there is no reentrant vulnerability to your contract. named parameter invocation and anonymous function arguments (Named Calls and Anonymous function paramters)
The parameters of the function call can be called by specifying the name, but can be contained in any order, in the way that {}. However, the type and number of parameters should be consistent with the definition.
pragma solidity ^0.4.0;
Contract C {
function Add (uint val1, UINT val2) returns (UINT) {return val1 + val2;}
function g () returns (UINT) {
//named arguments return
Add ({val2:2, val1:1});
}
omit function name (omitted function Parameter Names)
Parameter names that are not used can be omitted (generally common to return values). These names exist on the stack (stack) but are not accessible.
pragma solidity ^0.4.0;
Contract C {
//omitted name for parameter
function func (UI