Scope

The concept of Scope refers to where an object is valid, i.e. where it can be uniquely used. Sometimes referred to as its spatial region.

 

(*Note: The term Object is being used interchangeably for variables, etc)

 

An object is not valid until it has been declared.

 

An object becomes valid from the point at where it is declared.

 

An object with:

  • Local Scope
    • aka Block Scope
    • Has validity from the point within the block in which it was declared
    • i.e. within the {code block, between the curly braces }
    • Nested Block Scope
      • Inner object overrides outer object
      • i.e. if outer object has same name as inner object, only the inner object will be valid within that nested block
  • Function Scope
    • Object is valid from the start to the end of the function
    • Object should be uniquely identifiable
  • Global Scope
    • aka Program Scope
    • Objects declared outside of a function (including main)
    • Valid from the point at which it was declared
    • File scope
      • Another type of global scope
      • Objects are valid within the scope of the source file in which it was declared
      • Slightly different to program scope, whereby the object is valid within all other (included / linked in) files
      • e.g. variables declared static

 

Assuming no other same named object exists within the lower layers, an object with:

  • Program scope is valid within all layers
  • File scope is valid within the File, Function and Block layers
  • Function scope is valid within the Function and Block layers
  • Block scope is only valid within its own block

Illustration of visibility of scope, whereby outer layers can be seen by inner layers, but inner layers can not be seen by outer layers.

 

 

 

Program to illustrate scope, using a singly defined variable, theVar:

#include <iostream>
using namespace std ;

int theVar = 17 ;	//globally scoped

int add(int a, int b){	//function scope
	return ( a + b );
}

int main () {

	cout << "The value of theVar from the global scope is:" << theVar << endl ;

	int theVar = 3 ;	//locally scope
	cout << "The value of theVar in this outer block is: " << theVar << endl ;

	{ //the curly braces indicate another code block
		int theVar = 36 ;	//local scope
		cout << "The value of theVar in this inner block is: " << theVar << endl ;

		{ //these curly braces indicate another (nested) code block
			int theVar = 42 ;	//nested scope
			cout << "The value of theVar within this nested inner block is: " << theVar << endl ;
		}
	}

	//now use outer block value of the theVar, and send to a function
	cout << "Taking the outer block value of theVar (" << theVar << ") and adding its square (" \
			<< (theVar * theVar) << ") = " << add(theVar, (theVar * theVar) ) << endl ;

		return 0;
}

Compile & Run:

The value of theVar from the global scope is:17
The value of theVar in this outer block is: 3
The value of theVar in this inner block is: 36
The value of theVar within this nested inner block is: 42
Taking the outer block value of theVar (3) and adding its square (9) = 12

 

Notice that there is only one named variable called theVar. However we can uniquely act upon its multiple specific vales, depending on how and where it is declared and used.

Leave a Reply