diff options
Diffstat (limited to 'docs/control-structures.rst')
-rw-r--r-- | docs/control-structures.rst | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/docs/control-structures.rst b/docs/control-structures.rst index bcb597cf..0c5825bc 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -20,10 +20,10 @@ For example, suppose we want our contract to accept one kind of external calls with two integers, we would write something like:: - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract Simple { - function taker(uint _a, uint _b) { + function taker(uint _a, uint _b) public pure { // do something with _a and _b. } } @@ -36,10 +36,14 @@ The output parameters can be declared with the same syntax after the the sum and the product of the two given integers, then we would write:: - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract Simple { - function arithmetics(uint _a, uint _b) returns (uint o_sum, uint o_product) { + function arithmetics(uint _a, uint _b) + public + pure + returns (uint o_sum, uint o_product) + { o_sum = _a + _b; o_product = _a * _b; } @@ -95,11 +99,11 @@ Internal Function Calls Functions of the current contract can be called directly ("internally"), also recursively, as seen in this nonsensical example:: - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract C { - function g(uint a) returns (uint ret) { return f(); } - function f() returns (uint ret) { return g(7) + f(); } + function g(uint a) public pure returns (uint ret) { return f(); } + function f() internal pure returns (uint ret) { return g(7) + f(); } } These function calls are translated into simple jumps inside the EVM. This has @@ -125,13 +129,13 @@ the gas can be specified with special options ``.value()`` and ``.gas()``, respe pragma solidity ^0.4.0; contract InfoFeed { - function info() payable returns (uint ret) { return 42; } + function info() public payable returns (uint ret) { return 42; } } contract Consumer { InfoFeed feed; - function setFeed(address addr) { feed = InfoFeed(addr); } - function callFeed() { feed.info.value(10).gas(800)(); } + function setFeed(address addr) public { feed = InfoFeed(addr); } + function callFeed() public { feed.info.value(10).gas(800)(); } } The modifier ``payable`` has to be used for ``info``, because otherwise, the `.value()` @@ -180,11 +184,11 @@ parameters from the function declaration, but can be in arbitrary order. pragma solidity ^0.4.0; contract C { - function f(uint key, uint value) { + function f(uint key, uint value) public { // ... } - function g() { + function g() public { // named arguments f({value: 2, key: 3}); } @@ -198,11 +202,11 @@ Those parameters will still be present on the stack, but they are inaccessible. :: - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract C { // omitted name for parameter - function func(uint k, uint) returns(uint) { + function func(uint k, uint) public pure returns(uint) { return k; } } @@ -225,7 +229,7 @@ creation-dependencies are not possible. contract D { uint x; - function D(uint a) payable { + function D(uint a) public payable { x = a; } } @@ -233,11 +237,11 @@ creation-dependencies are not possible. contract C { D d = new D(4); // will be executed as part of C's constructor - function createD(uint arg) { + function createD(uint arg) public { D newD = new D(arg); } - function createAndEndowD(uint arg, uint amount) payable { + function createAndEndowD(uint arg, uint amount) public payable { // Send ether along with the creation D newD = (new D).value(amount)(arg); } @@ -270,16 +274,16 @@ Destructuring Assignments and Returning Multiple Values Solidity internally allows 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 and also assign them to multiple variables (or LValues in general) at the same time:: - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract C { uint[] data; - function f() returns (uint, bool, uint) { + function f() public pure returns (uint, bool, uint) { return (7, true, 2); } - function g() { + function g() public { // Declares and assigns the variables. Specifying the type explicitly is not possible. var (x, b, y) = f(); // Assigns to a pre-existing variable. @@ -326,10 +330,10 @@ As a result, the following code is illegal and cause the compiler to throw an er // This will not compile - pragma solidity ^0.4.0; + pragma solidity ^0.4.16; contract ScopingErrors { - function scoping() { + function scoping() public { uint i = 0; while (i++ < 1) { @@ -341,7 +345,7 @@ As a result, the following code is illegal and cause the compiler to throw an er } } - function minimalScoping() { + function minimalScoping() public { { uint same2 = 0; } @@ -351,7 +355,7 @@ As a result, the following code is illegal and cause the compiler to throw an er } } - function forLoopScoping() { + function forLoopScoping() public { for (uint same3 = 0; same3 < 1; same3++) { } @@ -364,9 +368,9 @@ In addition to this, if a variable is declared, it will be initialized at the be As a result, the following code is legal, despite being poorly written:: pragma solidity ^0.4.0; - + contract C { - function foo() returns (uint) { + function foo() public pure returns (uint) { // baz is implicitly initialized as 0 uint bar = 5; if (true) { @@ -412,7 +416,7 @@ and how ``assert`` can be used for internal error checking:: pragma solidity ^0.4.0; contract Sharer { - function sendHalf(address addr) payable returns (uint balance) { + function sendHalf(address addr) public payable returns (uint balance) { require(msg.value % 2 == 0); // Only allow even numbers uint balanceBeforeTransfer = this.balance; addr.transfer(msg.value / 2); |