局部作用域和函数作用域的区别

Difference between local scope and function scope

本文关键字:作用域 区别 局部 函数      更新时间:2023-10-16

一旦我认为这两个有相同的意思,但在阅读了更多关于它我仍然不清楚的区别。局部作用域有时不是指函数的作用域吗?只有标签有函数作用域是什么意思?

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

Function Scope between outer { }

局部作用域在内部{ }之间

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

只有标签有函数作用域是什么意思?

Labels只是标识符后面跟着一个冒号。标记语句用作goto语句的目标。标签可以在函数中出现的任何地方使用,但不能在函数体之外引用。因此,它们被称为具有函数作用域。

代码示例:

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

局部作用域是一个{和它的关闭}之间的区域。函数作用域是函数的开始{和结束}之间的区域,它可以包含更多的"局部"作用域。标签在定义它的整个函数中是可见的,例如

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在其封闭块外不可见。标签在其封闭块之外可见,但仅在函数范围内可见。

局部作用域有时不是指函数作用域吗?

是的。在大多数c派生语言中,变量在声明它们的范围内是有效的。如果在函数内部声明变量,而不是在任何其他代码块中声明变量,则该变量通常称为"局部"或"自动"变量。你可以在函数的任何地方引用它。另一方面,如果在另一个代码块中声明变量——比如在条件语句体中,那么该变量仅在该代码块中有效。这里还有几个很好的例子。

,只有标签有函数作用域是什么意思?

Context会有帮助,但这意味着你不能从一个函数跳转到另一个函数中的标签。

void foo(int a) {
    if (a == 0) goto here;  // okay -- 'here' is inside this function
    printf("a is not zeron");
    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 zeron");
there:
    return;
}

不是要捅马蜂窝,但标签的范围可能不会经常出现。标签主要用于goto语句,它很少需要,即使您选择使用goto,您可能甚至不会想到尝试跳转到另一个函数。

函数的作用域略大于函数的作用域:函数的实参在外部作用域,而局部变量只在内部作用域。这在function-try-block中最为明显:

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

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

当然,你也可以并且确实一直引入更深层次的嵌套作用域,例如在for循环体或条件体中。

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
}