120 likes | 264 Views
Programming. Scope of Identifiers. Scope. A sequence of statements within { … } is considered a block of code. The part of the program where you can use a certain identifier is called the scope of that identifier.
E N D
Programming Scope of Identifiers
Scope • A sequence of statements within { … } is considered a block of code. • The part of the program where you can use a certain identifier is called the scope of that identifier. • The scope of an identifier starts immediately after its declaration and ends when the “innermost” block of code within which it is declared ends. • It is possible to declare the same identifier in another block within the program.
Scope • The scope of an identifier does not apply if the same identifier is declared in an inner block. • A global declaration of an identifier is made outside the bodies of all functions, including the main function. It is normally grouped with the other global declarations and placed at the beginning of the program file. • A local declaration of an identifier is made inside a block of code which could be the body of a function. • Globally declared identifiers can be accessed anywhere in the program. • Locally declared identifiers cannot be accessed outside of the block they were declared in.
Scope: Example 1 int y = 38; void fun(int, int); int main( ){ int z=47; while(z<400){ int a = 90; z += a++; z++; } y = 2 * z; fun(1, 2); return 0; } void fun(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } scope of y scope of fun scope of z scope of a scope of s & t scope of r scope of i
Example 2:Global Constants #include <math.h> #include <iostream> using namespace std; const double PI = 3.14159; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //returns the volume of a sphere with the specified radius. int main(){ double radius_of_both, area_of_circle, volume_of_sphere. cout << " Enter a radius to use for both a circle " << " and a sphere (in inches): “ cin >> radius_of_both; area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both);
Example 2:Global Constants cout << "Radius = " << radius_of_both << " inches \n" << "Area of circle = " << area_of_circle <<" square inches\n" <<"Volume of sphere = " << volume_of_sphere <<" cubic inches\n"; return 0; } double area(double radius) { return(PI * radius * radius); } double volume(double radius) { return((4.0/3.0)* PI * radius * radius * radius); }
Scope: Example 3 • Number in Increment() is the global variable. #include <iostream> using namespace std; intNumber; //global variable void Increment(int IncNum) { IncNum = IncNum + 3; cout << IncNum << endl; Number = Number + 1; } int main() { Number = 1; Increment(Number); cout << Number << endl; return 0; }
Scope: Example 4 int Number; //global variable void Increment(int& IncNum) { IncNum = IncNum + 3; cout << IncNum << endl; Number = Number + 1; } int main() { Number = 1; Increment(Number); cout << Number << endl; return 0; } • When Increment is called, IncNum refers to global variable Number. • Number = Number + 1 also refers to global variable Number.
Scope: Example 5 intNumber; //global variable void Increment(intNumber) { Number = Number + 1; cout << Number << endl; } int main() { Number = 1; Increment(Number); cout << Number << endl; return 0; } • The scope of the global variable Number does not include Increment(), because Increment() already has a local parameter of the same name. • Thus, the changes made to Number are lost when control returns to the main program.
Scope: Example 6 int A,B,C,D; void Two(int A, int B, int& D) { B = 21; D = 23; cout << A << " " << B << " " << C<< " " << D << endl; } void One(int A, int B, int& C) { int D; // Local variable A = 10; B = 11; C = 12; D = 13; cout << A << " " << B<< " " << C<< " " << D << endl; Two(A,B,C); } int main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C); cout << A << " " << B << " " << C<< " " << D << endl; Two(A,B,C); cout << A << " " <<B << " " << C << " " << D << endl; return 0; }
Scope: Example 6 • Output: ABCD in One = 10 11 12 13 ABCD in Two = 10 21 23 23 ABCD in Main = 1 2 23 4 ABCD in Two = 1 21 23 23 ABCD in Main = 1 2 23 4
Global Variables • Undisciplined use of global identifiers may lead to confusion and debugging difficulties. • Instead of using global variables, you should communicate values between functions through the arguments in function calls.