Coupon Accepted Successfully!


Scope Rules

  • The scope rules that we are using now are variables without formally discussing where they can be declared, how long they remain in existence and what part of a program have access to them.

  • There are different types of scope available in C++.

  • They are

    • Local scope

    • Global scope

Local Scope

  • A local scope is created by a block.

  • Thus each time you start a new block, you are creating a new scope.

  • A variable can be declared with in any block.

  • A variable that is declared inside a block is called local variable.

  • A local variable can be used only by the statements with in the block in which it is declared.

  • A local variable is created when its declaration statement is encountered within its block, and destroyed when the block is left.

Global Scope

  • Since local scope is known only with in the function in which they are declared.

  • But the global scope is the declarative region that is outside of the entire region.

  • Declaring a variable in the global scope creates a new global variable.

  • Global variable is known through out the entire program.

  • Storage for global variable is in a fixed region of memory set aside for this purpose by the program.

  • Global variable is helpful when the same data is used by several functions in your program.

Note: The time interval for which a particular variable or data value lives in the memory is called Lifetime of a variable or data value.

Test Your Skills Now!
Take a Quiz now
Reviewer Name