40 - Scoping
Scoping in Solidity follows the widespread scoping rules of C99
-
Variables are visible from the point right after their declaration until the end of the smallest
{ }
-block that contains the declaration. As an exception to this rule, variables declared in the initialization part of a for-loop are only visible until the end of the for-loop. -
Variables that are parameter-like (function parameters, modifiers parameters, catch parameters, …) are visible inside the code block that follows - the body of the function/modifier for a function and modifier parameter and the catch block for a catch parameter.
-
Variables and other items declared outside of a code block, for example functions, contracts, user-defined types, etc., are visible even before they were declared. This means you can use state variables before they are declared and call functions recursively.
- Variable Visibility
- C99 Scoping
- Declaration -> End of
{}
- Parameter -> Code Block
- Functions/Contracts/State Variables: Visible Before Declaration
- Security: Data Flow Analysis