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

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Walter Savitch Chapter 3 Procedural Abstraction and Functions That Return a Value Slides by David B Teague, Western Carolina University, A constituent.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Programming Functions: Passing Parameters by Reference.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Chapter 4 Summation.
Functions:Passing Parameters by Value Programming.
Computer Science 1620 Function Scope & Global Variables.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Function Part II: Some ‘advanced’ concepts on functions.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Functions Modules in C++ are called functions and classes
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
1 ACS 168 Structured Programming Using the Computer Spring 2002 By Joaquin Vila Prepared by Shirley White.
Programming Functions: Passing Parameters by Reference.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to Programming
Fundamentals of structural programming
Predefined Functions Revisited
School of EECS, Peking University
FUNCTIONS IN C++.
Global & Local Identifiers
More ‘concepts’ on Function
Namespaces How Shall I Name Thee?.
Function “Inputs and Outputs”
Local, Global Variables, and Scope
The Function Prototype
Predefined Functions Revisited
More ‘concepts’ on Function
Programming Fundamental
Presentation transcript:

Programming Scope of Identifiers

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.

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.

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

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

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); }

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; }

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.

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.

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; }

COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 11 Scope: Example 6 l Output: ABCD in One = ABCD in Two = ABCD in Main = ABCD in Two = ABCD in Main =

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.