csci 130 scope of variables chapter 6. what is scope parts of program which can access a variable...

13
CSCI 130 Scope of Variables Chapter 6

Upload: blaise-summers

Post on 29-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

CSCI 130

Scope of Variables

Chapter 6

Page 2: 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

What is scope

• Parts of program which can access a variable– accessibility– visibility

• How long variable takes up system resources (memory)

Page 3: 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

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

} |

Page 4: 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

Importance of scope

• Modularization– each function should be independent– variables isolated from interference

• Scope allows for control over degree of isolation

Page 5: 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

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)

Page 6: 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

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;

}

Page 7: 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

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

Page 8: 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

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

} |

Page 9: 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

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

Page 10: 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

Static Variables

• Static external variables apply only to variables within the file

static float pi = 3.14;

void main() {

….

Area = pi * radius * radius;

}

Page 11: 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

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.

Page 12: 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

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

Page 13: 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

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.