ABSTRACT

Chapter 2 described several rules. One of those rules was that each function can see only its own frame. This is called scope. A new scope is created every time a pair of { and } is used. This could be inside of a function body, for example, an if statement, or a while loop. The following example shows two scopes:

void f( int a, int b)1 {2

/* this is a scope , call it X */3

int i;4 for (i = 0; i < a + b; i ++)5

{6

/* this is another scope , call it Y */7

int j;8 }9

}10

i is a nested each other like this. Variables from outer scopes are still accessible, so scope Y can “see” a, b, and i. A local variable j is created inside of scope Y and is accessible only inside scope Y; scope X cannot “see” j.