Monday, August 17, 2009

Variable scope

The scope of a variable is the section of a program where the variable can be used. The scope depends on where the variable is declared:
  • If a variable is declared within a block, its scope runs from its declaration to the end of the block
  • If a variable is declared outside all blocks (global variable), its scope runs from its declaration to the end of the program


Note that within the scope of the variable other variables with the same name cannot be declared. If the compilers encounters a second variable declaration with the same name it generates an error because of a name conflict.


Previous post

Next post




No comments:

Post a Comment