The difference between the local area and the scale of the function

As soon as I assumed that the two have the same meaning, but after I read more about it, I still do not quite understand this difference. Is the local area sometimes not in scope? and what does it mean that only labels have a function scope?

+4
source share
5 answers
void doSomething() { <------- { <---- | | | int a; Local Scope Function Scope | | } <---- | } <------- 

The Scope function is between the external { } .

Local area between internal { }

Please note that any area created by {``} can be called as a local area, and {``} at the beginning of the function body creates a scope.
So, sometimes the local area can be the same as the area of ​​functions.

which means that only labels have a function scope?

Labels are nothing more than identifiers followed by a colon. The indicated statements are used as targets for goto . Labels can be used anywhere in the function in which they appear, but cannot be specified outside the function body. Therefore, they say that they have a functional area.

Code example:

 int doSomething(int x, int y, int z) { label: x += (y + z); /* label has function scope*/ if (x > 1) goto label; } int doSomethingMore(int a, int b, int c) { if (a > 1) goto label; /* illegal jump to undefined label */ } 
+15
source

A local area is the area between {and close}. A scope is the area between the opening of a {function and its closure}, which may contain more "local" areas. The label is displayed in the entire function in which it is defined, for example.

 int f( int a ) { int b = 8; if ( a > 14 ) { int c = 50; label: return c - a - b; } if ( a > 7 ) goto label; return -99; } 

int c not displayed outside its block. the label is visible outside its enclosing block, but only for the scope.

+4
source

Does the local area belong to the scope?

Yes. In most languages ​​of C-variables, variables are valid in the area in which they are declared. If you declare a variable inside a function, but not inside any other block of code, then this variable is usually called a "local" or "automatic" variable. You can access it anywhere in the function. On the other hand, if you declare your variable inside another block of code - say, in the body of a conditional statement, then the variable is valid only inside this block. A few other answers here give good examples.

and what does it mean that only labels have a function scope?

A context would be useful, but that means you cannot go from one function to a label in another function.

 void foo(int a) { if (a == 0) goto here; // okay -- 'here' is inside this function printf("a is not zero\n"); goto there; // not okay -- 'there' is not inside this function here: return; } void bar(int b) { if (b == 0) goto there; // okay -- 'there' is in this function printf("b is not zero\n"); there: return; } 

Do not arouse the hornet's nest, but the volume of the labels probably will not appear too often. Shortcuts are mostly useful with the goto , which is very rarely needed, if ever, and even if you decided to use goto , you probably wouldn't even think about trying to switch to another function.

+2
source

The scope of the function is slightly larger than the area of ​​the body of the function: the arguments of the function are in the outer region, and local variables are only in the inner. This is most evident in the try-block function:

 void f(int a) try { // function body } catch(...) { // catch block } 

Inside a catch block, only variables in the function scope are still in scope, but not local variables.

Of course, you can and also introduce ever new, deeper nested areas all the time, for example. in for contour bodies or conditional bodies.

+1
source
 bool m[3][3]; void f1() { int i; // redefining a variable with the same name in the same scope isn't possible //int i; //error C2086: 'int i' : redefinition } void f2() { int i; // ok, same name as the i in f1(), different function scope. { int i; // ok, same name as the i above, but different local scope. } // the scope if the following i is local to the for loop, so it ok, too. for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (m[i][j]) goto loopExit; } } loopExit: std::cout << "done checking m"; // redefining a label with the same name in the same function isn't possible // loopExit:; // error C2045: 'loopExit' : label redefined } void f3() { loopExit:; // ok, same label name as in f2(), but different function scope } 
+1
source

Source: https://habr.com/ru/post/919762/


All Articles