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.

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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Computer Science 1620 Loops.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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 Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
C++ Pointer and Functions
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Functions:Passing Parameters by Value Programming.
Computer Science 1620 Function Scope & Global Variables.
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 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Chapter 6: Functions.
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).
Functions g g Data Flow g Scope local global part 4 part 4.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Pointers & Functions.
Anatomy of a Function Part 1
More ‘concepts’ on Function
Pass by Reference.
Local, Global Variables, and Scope
The Function Prototype
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Anatomy of a Function Part 1
More ‘concepts’ on Function
Programming Fundamental
Presentation transcript:

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 a,b,c; … } int two(bool a, …) { char I,j,k; … } void f1(int a, …) { double x,y,z; … } void f2(double a, …) { bool x,y,z; }

COMP104 Slide 3 Scope The scope of a declaration is the block of code where the identifier is valid for use. n A global declaration is made outside the bodies of all functions and outside the main program. It is normally grouped with the other global declarations and placed at the beginning of the program file. n A local declaration is one that is made inside the body of a function. Locally declared variables cannot be accessed outside of the function they were declared in. Local to a function (the variables in Main are also local, local to ‘main’ function) n It is possible to declare the same identifier name in different parts of the program : local to a block Some code enclosed in braces

COMP104 Slide 4 Local variables to a function int main() { int x,y,z; … } void f() { int x; … }

COMP104 Slide 5 Formal parameters are local to a function * All variables/constants in the formal parameters and inside the body are local to the function * They can not be used by others * They are short-lived: they come when the function is called, and go when the function returns.

COMP104 Slide 6 Local to a block void f() { int x; x=1; { int a; a=2; cout << a << endl; cout << x << endl; } cout << a << endl; cout << x << endl; } ?

COMP104 Slide 7 Always, local first void f() { int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } ?

COMP104 Slide 8 In a for-loop { int i; for (i=1;i<10;i++) cout << A[i]; } for (int i=1;i<10;i++) cout << A[i]; equivalent

COMP104 Slide 9 Block scope is local * within a pair of braces {…} n For, while, do-while, if, else, switch, etc … * All variables/constants in the block are local to the block * They can not be used out of the block * They are short-lived as well: they come when the block is entered, and go when the block is finished.

COMP104 Slide 10 Global variables int x; int main() { x=0; cout << x << endl; int b; b=1; { int c; c=2; cout << c << endl; } cout << c << endl; }

COMP104 Slide 11 Global is ‘file’ scope l Global variables are initialized to 0 when not explicitly initialized l Global variable can be accessed by anyone in the same file l When a global variable is used in a different file, it needs to have a ‘external declaration’ l Functions are all ‘Global’ l When a function is used in a different file, it needs to be re-declared

COMP104 Slide 12 Who’s who? int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; }

COMP104 Slide 13 Identifier name and conflict resolution * An identifier can be defined only once in the same scope n We can not have two variables/constants of the same name even they have different types n Wrong: int x=1; double x=1.0; * But, the same identifier can be ‘re-used’ in different scopes. * When an identifier is declared more than once, that identifier in the innermost scope is selected by the compiler.  local first!!!

COMP104 Slide 14 Example 1  Number in Increment () is the global variable. #include using namespace std; int Number; //global variable void Increment(int Num) { Num = Num + 1; cout << Num << endl; Number = Number + 1; } void main() { Number = 1; Increment(Number); cout << Number << endl; }

COMP104 Slide 15 Example 2 int Number; //global variable void Increment(int& Num) { Num = Num + 1; cout << Num << endl; Number = Number + 1; } void main() { Number = 1; Increment(Number); cout << Number << endl; } * When Increment is called, Num refers to global variable Number * Number = Number + 1 also refers to global variable Number.

COMP104 Slide 16 Example 3 Number int Number; //global variable void Increment(int Number) { Number = Number + 1; cout << Number << endl; } void main() { Number Number = 1; Number Increment(Number); cout << Number << endl; } * 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.

COMP104 Slide 17 Global Variables * Undisciplined use of global variables may lead to confusion and debugging difficulties. * Instead of using global variables in functions, try passing local variables by reference.

COMP104 Slide 18 Example 4 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); } void 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; }

COMP104 Slide 19 * Output:

COMP104 Slide 20 int y = 38; void f(int, int); void main( ){ int z=47; while(z<400){ int a = 90; z += a++; z++; } y = 2 * z; f(1, 2); } void f(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } Scope resolution example scope of i scope of r scope of s & t scope of a (local to while-block) scope of z scope of y (global) scope of f

COMP104 Slide 21 int MIN; void min(int,int); int main() { int x,y; cin >> x >> y >> endl; min(x,y); cout << MIN; } void min(int a, int b) { if (a<b) MIN=a; else MIN=b; } void min(int,int,int&); int main() { int x,y,mini; cin >> x >> y >> endl; min(x,y,mini); cout << mini; } void min(int a, int b, int& m) { if (a<b) m=a; else m=b; } int min(int,int); int main() { int x,y,mini; cin >> x >> y >> endl; mini=min(x,y); cout << mini; } int min(int a, int b) { int m; if (a<b) m=a; else m=b; return (m); } Summary Global variable Pass by referencePass by value

COMP104 Slide 22 int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } Summary global local (to the main) local (to the block)

COMP104 Slide 23 n Global vs. local n Function scope n Block scope n Global variables (forbidden!!!) n Everything is relative  File scope!