Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.

Similar presentations


Presentation on theme: "Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered."— Presentation transcript:

1 Programming Scope of Identifiers

2 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered a block of code. l The part of the program where you can use a certain identifier is called the scope of that identifier. l The scope of an identifier starts immediately after its declaration and ends when the “innermost” block of code within which it is declared ends. l It is possible to declare the same identifier in another block within the program.

3 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 3 Scope n The scope of an identifier does not apply if the same identifier is declared in an inner block. n 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. n A local declaration of an identifier is made inside a block of code which could be the body of a function. n Globally declared identifiers can be accessed anywhere in the program. n Locally declared identifiers cannot be accessed outside of the block they were declared in.

4 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 4 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: Example 1 scope of i scope of r scope of s & t scope of a scope of z scope of y scope of fun

5 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 5 Example 2:Global Constants #include 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);

6 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 6 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); }

7 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 7 Scope: Example 3 Number in Increment () is the global variable. #include using namespace std; 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; }

8 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 8 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; } l When Increment is called, IncNum refers to global variable Number. l Number = Number + 1 also refers to global variable Number.

9 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 9 Scope: Example 5 int Number; //global variable void Increment(int Number) { 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.

10 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 10 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; }

11 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 11 Scope: Example 6 l 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

12 COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 12 Global Variables l Undisciplined use of global identifiers may lead to confusion and debugging difficulties. l Instead of using global variables, you should communicate values between functions through the arguments in function calls.


Download ppt "Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered."

Similar presentations


Ads by Google