局部范围和功能范围之间的差异

时间:2022-01-11 03:38:48

Once I assumed that these two have the same meaning but after reading more about it i'm still not clear about the difference. Doesn't the local scope sometimes refer to scope of function? and what does it mean that only labels have a function scope?

一旦我认为这两个具有相同的含义,但在阅读了更多关于它之后,我仍然不清楚这些差异。局部范围有时是指功能范围吗?什么意思只有标签有功能范围?

5 个解决方案

#1


15  

void doSomething()
{                                    <-------
     {                   <----               | 
                              |              |
         int a;           Local Scope    Function Scope
                              |              |
     }                   <----               | 
}                                    <------- 

Function Scope is between outer { }.

函数范围在外部{}之间。

Local scope is between inner { }

本地范围介于内部{}之间

Note that, any scope created by {``} can be called as the local scope while the {``} at the beginning of the function body create the Function scope.
So, Sometimes a Local Scope can be same as Function Scope.

注意,{``}创建的任何作用域都可以作为局部作用域调用,而函数体开头的{``}可以创建函数作用域。因此,有时本地范围可以与功能范围相同。

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

只有标签有功能范围是什么意思?

Labels are nothing but identifiers followed by a colon. Labeled statements are used as targets for goto statements. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Hence they are said to have Function Scope.

标签只是标识符后面跟冒号。标记语句用作goto语句的目标。标签可以在它们出现的函数中的任何位置使用,但不能在函数体外部引用。因此他们被认为具有功能范围。

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 */
}

#2


4  

Local scope is the area between an { and it's closing }. Function scope is the area between the opening { of a function and its closing }, which may contain more "local" scopes. A label is visible in the entirety of the function within which it is defined, e.g.

局部范围是{和它的关闭}之间的区域。函数范围是函数的开头{和它的结束}之间的区域,它可能包含更多的“本地”范围。标签在其定义的整个功能中是可见的,例如,标签。

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 is not visible outside its enclosing block. label is visible outside its enclosing block, but only to function scope.

int c在其封闭块之外是不可见的。标签在其封闭块外可见,但仅限于功能范围。

#3


2  

Doesn't the local scope sometimes refer to scope of function?

局部范围有时是指功能范围吗?

Yes. In most C-derived languages, variables are valid in the scope in which they're declared. If you declare a variable inside a function, but not within any other code block, then that variable is usually called a "local" or "automatic" variable. You can refer to it anywhere in the function. On the other hand, if you declare your variable inside another code block -- say, in the body of a conditional statement, then the variable is valid only inside that block. Several other answers here give good examples.

是。在大多数C派生语言中,变量在声明它们的范围内有效。如果在函数内部声明变量,但不在任何其他代码块中声明,则该变量通常称为“本地”或“自动”变量。您可以在函数的任何位置引用它。另一方面,如果您在另一个代码块中声明您的变量 - 比如在条件语句的主体中,那么该变量仅在该块内有效。其他几个答案给出了很好的例子。

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

什么意思只有标签有功能范围?

Context would be helpful, but it means that you can't jump from one function to a label in a different 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;
}

Not to stir up a hornet's nest, but the scope of labels probably won't come up too often. Labels are mainly useful with the goto statement, which is needed only very rarely if ever, and even if you did choose to use goto you probably wouldn't even think of trying to jump into a different function.

不要激怒大黄蜂的巢穴,但标签的范围可能不会经常出现。标签主要用于goto语句,如果有的话很少需要它,即使你确实选择使用goto,你甚至可能不会想到尝试跳转到另一个函数。

#4


1  

The scope of the function is slightly larger than the scope of the function body: The function arguments are in the outer scope, while local variables are only in the inner one. This is most visibly manifest in a function-try-block:

函数的作用域略大于函数体的作用域:函数参数在外部作用域中,而局部变量仅在内部作用域中。这在函数try块中最明显:

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

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

在catch块内部,只有函数作用域中的变量仍在范围内,而不是局部变量。

Of course you can and do also introduce further, deeper nested scopes all the time, e.g. in for loop bodies or conditional bodies.

当然,您可以并且还可以一直引入更深入的嵌套范围,例如: in for循环体或条件体。

#5


1  

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's 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


15  

void doSomething()
{                                    <-------
     {                   <----               | 
                              |              |
         int a;           Local Scope    Function Scope
                              |              |
     }                   <----               | 
}                                    <------- 

Function Scope is between outer { }.

函数范围在外部{}之间。

Local scope is between inner { }

本地范围介于内部{}之间

Note that, any scope created by {``} can be called as the local scope while the {``} at the beginning of the function body create the Function scope.
So, Sometimes a Local Scope can be same as Function Scope.

注意,{``}创建的任何作用域都可以作为局部作用域调用,而函数体开头的{``}可以创建函数作用域。因此,有时本地范围可以与功能范围相同。

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

只有标签有功能范围是什么意思?

Labels are nothing but identifiers followed by a colon. Labeled statements are used as targets for goto statements. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Hence they are said to have Function Scope.

标签只是标识符后面跟冒号。标记语句用作goto语句的目标。标签可以在它们出现的函数中的任何位置使用,但不能在函数体外部引用。因此他们被认为具有功能范围。

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 */
}

#2


4  

Local scope is the area between an { and it's closing }. Function scope is the area between the opening { of a function and its closing }, which may contain more "local" scopes. A label is visible in the entirety of the function within which it is defined, e.g.

局部范围是{和它的关闭}之间的区域。函数范围是函数的开头{和它的结束}之间的区域,它可能包含更多的“本地”范围。标签在其定义的整个功能中是可见的,例如,标签。

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 is not visible outside its enclosing block. label is visible outside its enclosing block, but only to function scope.

int c在其封闭块之外是不可见的。标签在其封闭块外可见,但仅限于功能范围。

#3


2  

Doesn't the local scope sometimes refer to scope of function?

局部范围有时是指功能范围吗?

Yes. In most C-derived languages, variables are valid in the scope in which they're declared. If you declare a variable inside a function, but not within any other code block, then that variable is usually called a "local" or "automatic" variable. You can refer to it anywhere in the function. On the other hand, if you declare your variable inside another code block -- say, in the body of a conditional statement, then the variable is valid only inside that block. Several other answers here give good examples.

是。在大多数C派生语言中,变量在声明它们的范围内有效。如果在函数内部声明变量,但不在任何其他代码块中声明,则该变量通常称为“本地”或“自动”变量。您可以在函数的任何位置引用它。另一方面,如果您在另一个代码块中声明您的变量 - 比如在条件语句的主体中,那么该变量仅在该块内有效。其他几个答案给出了很好的例子。

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

什么意思只有标签有功能范围?

Context would be helpful, but it means that you can't jump from one function to a label in a different 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;
}

Not to stir up a hornet's nest, but the scope of labels probably won't come up too often. Labels are mainly useful with the goto statement, which is needed only very rarely if ever, and even if you did choose to use goto you probably wouldn't even think of trying to jump into a different function.

不要激怒大黄蜂的巢穴,但标签的范围可能不会经常出现。标签主要用于goto语句,如果有的话很少需要它,即使你确实选择使用goto,你甚至可能不会想到尝试跳转到另一个函数。

#4


1  

The scope of the function is slightly larger than the scope of the function body: The function arguments are in the outer scope, while local variables are only in the inner one. This is most visibly manifest in a function-try-block:

函数的作用域略大于函数体的作用域:函数参数在外部作用域中,而局部变量仅在内部作用域中。这在函数try块中最明显:

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

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

在catch块内部,只有函数作用域中的变量仍在范围内,而不是局部变量。

Of course you can and do also introduce further, deeper nested scopes all the time, e.g. in for loop bodies or conditional bodies.

当然,您可以并且还可以一直引入更深入的嵌套范围,例如: in for循环体或条件体。

#5


1  

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's 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
}