Local, Global Variables, and Scope

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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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.
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.
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.
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.
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.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
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.
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.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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.
C++ Lesson 1.
Review 1.
Chapter 7 User-Defined Methods.
Bill Tucker Austin Community College COSC 1315
Chapter 5 Function Basics
School of EECS, Peking University
Chapter 5 Functions.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-defined Functions
Lab 1 Introduction to C++.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
User-defined Functions
Chapter 5 Function Basics
Pointers & Functions.
Anatomy of a Function Part 1
More ‘concepts’ on Function
Lab 1 Introduction to C++.
Introduction to Programming
Pass by Reference.
Namespaces How Shall I Name Thee?.
The Function Prototype
Fundamental Programming
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
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

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;

Scope The scope of a declaration is the block of code where Some code enclosed in braces The scope of a declaration is the block of code where the identifier is valid for use. 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. 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) It is possible to declare the same identifier name in different parts of the program: local to a block

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

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.

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

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

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

Block scope is local within a pair of braces {…} 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.

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

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

Who’s who? int x; int main() { x=0; cout << x << endl; }

Identifier name and conflict resolution An identifier can be defined only once in the same scope We can not have two variables/constants of the same name even they have different types 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!!!

Example 1 Number in Increment() is the global variable. #include <iostream> 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;

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.

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

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.

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; Two(A,B,C); void main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C);

Output: 10 11 12 13 10 21 23 23 1 2 23 4 1 21 23 23

Scope resolution example 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 of y (global) scope of f scope of z scope of a (local to while-block) scope of s & t scope of r scope of i

Summary Pass by reference Pass by value Global variable 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); 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; 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;

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

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