Introduction to Scope Rules of names or identifiers

A scope is a part of the program where a variable name or an identifier can be used. There are mainly six main forms of scopes that we will be concerned with -Local scope, global namespace scope, function scope, function prototype scope, class scope, and namespace scope. Since we are dealing with functions here will handle the first four and the other two shall be covered later.

Local Scope

An identifier declared inside the body of the function has a local scope. The scope of such an identifier begins at a point where it is declared and end with the terminating right brace (}) of the block of that function. variables declared in a function and the parameters of that function have a local scope i.e, they can only be accessed within and by that function. In cases where there is nesting, only the value of the inner variable can be accessed inside the nest. The outer varaible is hidden and can only be accessed only after the inner nest terminates. Please follow the example to understand the concept of a local scope of variables;
#include<iostream>
using namespace std;
void printX()
{
int x=7; //local variable x can only be accessed between this pont and the closing brace (})
cout<<"x is "<<x <<endl; } //end printX, beyond this point x cannot be accessed
void re_printX()
{ //x cannot be accessed here because it had been declared as a local function in printX, try removing the comments, the compiler will issue an error
//cout<< x is "<<x<<endl;
int x=5; int y=6; if ( y!=5) { int z=2; int x=z; // this x is the one that can be acced betwen this point and within the nest
cout<<"x nested in the if statement is " <<x<<endl; // it is the nested x that will print and after at the closing brace it will go out of scope }
cout<< "now x after the nested statement is "<<x<<endl; //the outer x is now accessible, it is the one to print
}
int main()
{ printX ();
re_printX ();
return 0; }

global namespace scope

A name or identifier declared outside a function is accessible from that point onwards, even within and outside any function therein until the end of the file. Such an identifier is said to have a global namespace scope.
Below is an example on the concept of global namespace scope;
#include<iostream>
using namespace std;
void printX(); //This prototype of function printx() makes the function have a global scope int w=7; //global variable w is declared will be accessed between this point until the end of the file
int y=6;
void printw() // function printw() is defined here as such any other will access it
{ //function printw, accesses variable w even when it is declared from outside
cout<<"The value of w as accessed by printw() is "<<w<<endl; }
int main() { printw ();
cout<<"main() is also able to access function printX()" <<" because its prototyoe is of global scope." <<"\nHere are contents of printX():"<<endl; printX ();
cout<<"Even main() is able to access w= " <<w<<" bacuse it has a gobal scope." <<endl; return 0; }
void printX() { cout<< "printX() also prints w as "<<w<< " becasue it has a global scope." << endl;
int x=5; if ( y!=5) { int y=4; cout<<"the value of y in internal block is " <<y<<" but using the Unary Scope Resolution Operator(::) the global value of y is " <<::y<<endl;
int z=2; int x=z; // this x is the one that can be acced betwen this point and within the nest it is the nested x that will print and after the closing brace it will go out of scope
cout<<"x nested in the if statement is "<<x<<endl;
} //the outer x is now accessible, it is the one to print
cout<< "now x after the nested statement is "<<x<<endl;
}
The value of w as accessed by printw()is 7 main() is also able to access function printX() because its prototyoe is of global scope. Here are contents of printX(): printX() also prints w as 7 becasue it has a global scope. the value of y in internal block is 4 but using the Unary Scope Resolution Operator(::) the global value of y is 6 x nested in the if statement is 2 now x after the nested statement is 5 Even main() is able to access w= 7 bacuse it has a gobal scope.
Unless you are to use a Unary Scope Resolution Operator (::), do not re-declare an identifier in an internal block as it would get hidden inside that block. Using the Unary Scope Resolution Operator (::) allows for accessing allows for accessing a gloable variable when the local variable of the same name is in scope. The operator however cannot be used to access a local variable of the same name outside the block. Identifiers with global namespace scope include all variables, function definitions and function prototypes declared outside a function.

Function Prototype scope

Parameter names included in a function prototype have a function prototype scope, i.e. they can be re used any where alse within the program without any ambiguity. This is possible because the compiler igonores parameters included in the function prototype. Most times programmers include parameters just to provide clarity even though the compiler is not meant to use them. If parameter names are included in a prototype each particular one must be used only once.

Function scope

indetifiers with a function scope are declared with a colon (as in myName:) so as to be accessed any where in a function within which they are declared although they cannot be referenced outside its body. Such indentifiers are known as labels and they are by functions to hide implementation details.
PREV:Introduction to functions


NEXT:Introduction to empty parameter list

No comments:

Post a Comment