diff options
author | Denton Liu <liu.denton+github@gmail.com> | 2016-06-23 01:25:26 +0800 |
---|---|---|
committer | Denton Liu <liu.denton+github@gmail.com> | 2016-06-23 01:35:15 +0800 |
commit | 0db67b88957c132aeda099d071faee55dbd6b616 (patch) | |
tree | 385e90a79f6fd50b970b84f1556f9aabdf7eb7c3 /docs/control-structures.rst | |
parent | b23c30079283095e54eb9678a88dda95d895dd6e (diff) | |
download | dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar.gz dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar.bz2 dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar.lz dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar.xz dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.tar.zst dexon-solidity-0db67b88957c132aeda099d071faee55dbd6b616.zip |
Add section about default values of variables
Diffstat (limited to 'docs/control-structures.rst')
-rw-r--r-- | docs/control-structures.rst | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/docs/control-structures.rst b/docs/control-structures.rst index 2f867cb0..fc55f8e6 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -151,9 +151,17 @@ Assigning *to* a state variable always creates an independent copy. On the other Scoping and Declarations ======================== -.. index:: ! scoping, ! declarations +.. index:: ! scoping, ! declarations, ! default-value -In Solidity, a variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. +In Solidity, a variable which is declared is automatically assigned its default value. It will be assigned on contract +initialization if it is a contract-level variable or at the beginning of a function call if it is a local variable. +This is because the EVM must run deterministically so it would be inappropriate to initialize any variables to random garbage values. +The "default values" of variables are the typical "zero-state" of whatever the type is. For example, the default value for a ``bool`` +is ``false``. The default value for the ``uint`` or ``int`` types is ``0``. For statically-sized arrays and ``bytes``, each individual +element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, ``bytes`` +and ``strings``, the default value is a zero-length member of its respective type. + +A variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. This happens because Solidity inherits its scoping rules from JavaScript. 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``:: |