130 likes | 259 Views
This chapter explores the concept of variable scope in programming, detailing how accessibility, visibility, and resource allocation affect variable usage. It compares the behavior of local and global variables, illustrating their implications through examples of compiling and non-compiling code. It emphasizes the significance of modularization and the isolation of variables to prevent interference. Additionally, it discusses static and register variables, outlining guidelines for effective variable management in programming. Learning these concepts enhances code clarity and efficiency.
E N D
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 system resources (memory)
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; | } } |
Importance of scope • Modularization • each function should be independent • variables isolated from interference • Scope allows for control over degree of isolation
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)
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; }
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
Local variables - example void main() { outputNumbers(); outputNumbers(); outputNumbers(); } ____________________________ void outputNumbers() { | Program output: static int x = 0; | 0 0 int y = 0; | 1 0 printf(“\n%d %d”, x++, y++); | 2 0 } |
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
Static Variables • Static external variables apply only to variables within the file • static float pi = 3.14; • void main() { • …. • Area = pi * radius * radius; • }
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.
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); • } • printf(“%d”, count); • }
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.