Difference between local scope and function scope

前端 未结 5 1707
独厮守ぢ
独厮守ぢ 2020-12-20 19:05

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 sc

5条回答
  •  礼貌的吻别
    2020-12-20 19:36

    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:

    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.

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

提交回复
热议问题