Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.

Similar presentations


Presentation on theme: "CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up."— Presentation transcript:

1 CSCI 130 Scope of Variables Chapter 6

2 What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up system resources (memory)

3 Scope Program 1 (compiles) | Program 2 (does not compile) int x = 999;|void squareX() | void squareX();|void main() { |int x = 999; void main() {|x = x * x; x = x * x;|} }|}| |squareX() { squareX () { |x = x * x; x = x * x;|} }|}|

4 Importance of scope Modularization –each function should be independent –variables isolated from interference Scope allows for control over degree of isolation

5 External (Global variables) Declared outside any function (including main) Visible to any functions within that file extern keyword –note: Listing 6.3 in book is incorrect –int x = 999; should be before main() Use globals for variables accessed by many functions (i.e. PI)

6 Example of Global variable int x = 999; void squareX(); void main() { extern int x;//This line is not necessary, but adds to structure x = x * x; } squareX () { extern int x; //This line is not necessary, but adds to structure x = x * x; }

7 Local Variables Defined within a function Visibility limited to that function Usually local variables created each time function is called, destroyed when function ends Can request variable not be destroyed –static keyword

8 Local variables - example void main() { outputNumbers(); } ____________________________ void outputNumbers() {|Program output: static int x = 0;|0 0 int y = 0;|1 0 printf(“\n%d %d”, x++, y++);|2 0 }|}|

9 Parameters and Scope A variable in the parameter list is local void function1(int x) { int y = 0; printf(“%d %d”, x, y); } In the preceding, x and y act as local variables

10 Static Variables Static external variables apply only to variables within the file static float pi = 3.14; void main() { …. Area = pi * radius * radius; }

11 Register variables Suggests that a local variable be stored in processor register (not regular memory) void function1() { register int x; …. } Any processing with x will be done quicker Can only be used with simple numeric variables –can’t use with arrays, structures, etc.

12 Program Blocks Variables can be declared as local within program blocks (statements enclosed in {}) void main() { int count = 0; printf(“%d”, count); { int count = 10; printf(“%d”, count); } }

13 Guidelines Initialize all variables Pass data as function parameters unless most functions use the data Use globals for data that is used in most of the functions Put definitions at beginning of scope –functions, files, etc.


Download ppt "CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up."

Similar presentations


Ads by Google