局部作用域和功能作用域之间的区别 [英] Difference between local scope and function scope

查看:124
本文介绍了局部作用域和功能作用域之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我认为这两个词具有相同的含义,但是在阅读了更多的内容后,我仍然不清楚其中的区别。本地范围有时不是指功能范围吗?
,这意味着只有标签才具有函数作用域?

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?

推荐答案

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

功能范围介于外部 { }

本地范围在内部<$ c之间$ c> { }

请注意,任何由<$ c创建的范围$ c> {``} 可以称为本地作用域,而在函数体开头的 {``} 创建函数

因此,有时Local Scope可以与Function Scope相同。

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?

标签只是标识符,后跟一个结肠。带标签的语句用作 goto 语句的目标。标签可以在显示功能的任何位置使用,但不能在功能主体外部引用。因此,它们被认为具有功能范围。

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.

代码示例:

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

这篇关于局部作用域和功能作用域之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆