Week 11 Multi-file Programs and Scope of Variables.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
1 Chapter 9 Scope, Lifetime, and More on Functions.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 10 Introduction to Classes
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
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.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
Functions g g Data Flow g Scope local global part II g Global Resolution Operator part II.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
#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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Classes C++ representation of an object
Functions Scope local global Global Resolution Operator part 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
A Lecture for the c++ Course
Functions Scope local global Global Resolution Operator part II
Storage class in C Topics Automatic variables External variables
CSC113: Computer Programming (Theory = 03, Lab = 01)
Global & Local Identifiers
Scope, Parameter Passing, Storage Specifiers
Chapter 9 Classes: A Deeper Look, Part 1
6 Chapter Functions.
Classes and Objects.
Storage class.
Scope Rules and Storage Types
Namespaces How Shall I Name Thee?.
Function “Inputs and Outputs”
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
STORAGE CLASS.
STORAGE CLASS.
The Three Attributes of an Identifier
C Programming Lecture-17 Storage Classes
More C++ Classes Systems Programming.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Storage Classes.
Presentation transcript:

Week 11 Multi-file Programs and Scope of Variables

Multi-file programs

Header files Creation of a header file math1.h // defining a header file: math1.h #ifndef math1 #define math1 #define M_PI 3.14 #endif

How to compile header files

Implementation files Don’t forget that the program allows to have one main() function only but many sub- functions. The main() function and other sub-functions can be put in either one file or different files. – Note that the main() function file does not need to #include those sub-function files; it incorporates those files by the function call mechanism.

Regions in a program There are three kinds of regions in a program according to their sizes, as shown in the diagram next slide. – File region – Function region – Block region.

Regions in a program

Scope of a variable Category 1: A variable declared once only in a region. Category 2: The variables with the same name declared in more than two regions separately

Category 1: the variable declared once only in a region

Case 1a : A variable declared in a block level region. Case 1b : A variable declared in a function level region. Case 1c : A variable declared in a file level region. Case 2a : A variable declared in a block level region Case 2b : A variable declared in a function level region. Case 2c : A variable declared in a file level region.

Case 1a – A variable declared in a block region ( file 1) #include void func1(); main() { // cout << "main(i)= " << i << endl; // it is error if this line is not masked func1(); } void func1() { // cout << "fun1(i)= " << i << endl; // it is error if this line is not masked { int i=7; cout << "block(i)= " << i << endl; // only this cout<< works }

Case 1b - A variable declared in a function region (file 1)

Case 1c- A variable declared in a file region (file 1)

A variable declared after the function in a file

Case 2a- A variable declared in a block region (file 2)

Case 2b – A variable declared in a function region (file 2)

Case 2c - A variable declared in a file region (file 2)

Category 2: the variables with the same name declared in two or more regions separately We have discussed the way to find the scope of a variable declared once only. From now on we are going to learn how to find the scope of the variables with the same name when they are declared in different regions at the same time. { int i; float i; } { int i; { float i; }

Scope of the variables with same name in different regions

The output values are: main(i) =1 (not 0) func2(i) =2 (not 0) block(i) =3 (not 0, or 2). In general: a variable in the inner region has a priority for use over those in the outer regions when they have same name.

Local variables and global variables We normally use a term global variables for those declared in either program level regions or in file level regions and another term local variables for those declared either in function level regions or in block level regions.

How to refer to a file scope variable from within a local block scope If you wish to access a file scope variable i from within a block in a different file, then you may use “extern i;” to get it, on the condition that no other local variable with the same has been defied within the block. However if this condition is not met or if there is the variable that has been already declared, can you still access that variable? The answer is yes but you need to use a special sign called “scope resolution operator ::” to do this job.

Scopes of auto variables (local) and local static variables All the variable declared within the functions and blocks are automatic variables by default. Prefacing the definition with an “auto” defines an automatic variable. When a block is entered the system allocates memory for the auto variables. Within that block, these variables are declared and are considered “local” to the block. When the block is left, the system releases the memory. Thus the values of these variables disappear. If the block is re-entered, the system once again allocates memory, and so previous values are unknown. However in most situations, we wish the system to allow a local variable to retain its previous value but not to initialise the variable (in spite of the initialisation statement ) when the block is re-entered. To do so, we need to define a new local variable called a static variable, which is made by prefacing “static” before the variable.

Scopes of auto variables (local) and local static variables

Scope of a global static variable extern i in file 2 is aimed to use static int i in file 1. This operation is illegal because “static” doesn’t allow the variable i to be used outside file 1 according to the sense of the static.