Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 11 Multi-file Programs and Scope of Variables.

Similar presentations


Presentation on theme: "Week 11 Multi-file Programs and Scope of Variables."— Presentation transcript:

1 Week 11 Multi-file Programs and Scope of Variables

2 Multi-file programs

3 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

4 How to compile header files

5 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.

6 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.

7 Regions in a program

8 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

9 Category 1: the variable declared once only in a region

10 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.

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

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

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

14 A variable declared after the function in a file

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

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

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

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

19 Scope of the variables with same name in different regions

20 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.

21 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.

22 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.

23 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.

24 Scopes of auto variables (local) and local static variables

25 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.


Download ppt "Week 11 Multi-file Programs and Scope of Variables."

Similar presentations


Ads by Google