diff options
author | Denton Liu <liu.denton+github@gmail.com> | 2016-05-12 05:07:25 +0800 |
---|---|---|
committer | Denton Liu <liu.denton+github@gmail.com> | 2016-05-12 05:07:25 +0800 |
commit | 52a8d7b3f5cbf3538d0c4e9f60446ebb59fb72ff (patch) | |
tree | 1b1b8ee7441f4d95600e1d560aef54fecdf60623 /docs | |
parent | 08b7d4aebe0c576d06bb9c8c73fdd21a2044ccb8 (diff) | |
download | dexon-solidity-52a8d7b3f5cbf3538d0c4e9f60446ebb59fb72ff.tar.gz dexon-solidity-52a8d7b3f5cbf3538d0c4e9f60446ebb59fb72ff.tar.zst dexon-solidity-52a8d7b3f5cbf3538d0c4e9f60446ebb59fb72ff.zip |
Added content
Diffstat (limited to 'docs')
-rw-r--r-- | docs/control-structures.rst | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/docs/control-structures.rst b/docs/control-structures.rst index 77fbee52..0ad1c591 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -147,6 +147,57 @@ Scoping and Declarations .. index:: ! scoping, ! declarations +In Solidity, a variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. +This is in contrast to many languages where variables are only scoped where they are declared until the end of the semantic block. +As a result, the following code is illegal and cause the compiler to throw an error, `Identifier already declared.`:: + + contract ScopingErrors { + function scoping() { + uint i = 0; + + while (i++ < 1) { + uint same1 = 0; + } + + while (i++ < 2) { + uint same1 = 0;// Illegal, second declaration of same1 + } + } + + function minimalScoping() { + { + uint same2 = 0; + } + + { + uint same2 = 0;// Illegal, second declaration of same2 + } + } + + function forLoopScoping() { + for (uint same3 = 0; same3 < 1; same3++) { + } + + for (uint same3 = 0; same3 < 1; same3++) {// Illegal, second declaration of same3 + } + } + } + +In addition to this, if a variable is declared, it will be initialized at the beginning of the function to its default value. +As a result, the following code is legal, despite being poorly written.:: + + function foo() returns (uint) { + uint bar = 5; + // baz is implicitly initialized as 0 + if (true) { + bar += baz; + } + else { + uint baz = 10;// never executes + } + return bar;// returns 5 + } + Exceptions ========== |