Difference between global and local scope

I have recently come across the concept of global and local scope and, I am having great difficulty understanding them in terms of conceptualization. What would be scope in its definition, and global and local scope?

Author: Maniero, 2020-05-06

2 answers

The question does not speak of specific languages so beware because this exact definition may vary a little from language to language. The general is the same, but the details change.

I've talked about scope in What's the difference between scope and lifetime?.

Global

Global scope applies throughout your application, any part of it running has access to global scope variables.

They are often dangerous and should be avoided. It is not you can not do, but you need to understand what you are doing, know that she can confuse with other things, she is circulating around and visible anywhere. In addition to name conflicts can have concurrency issues when the application allows this.

Local

Local scope means that the scope is limited. The most common is to be a function, so it is only visible within that function and is not confused with other parts of the code. I think you should imagine that this variable is easier to manage so you know where to look to understand where it can be modified, does not have the risk of the change coming from all locations.

If you access a global variable locally it can be confused with a local one. The compiler has some criteria to define whether it is accessing the local or global of the same name. In general it has some mechanism for resolving ambiguity, but not always. It is more common to have a way to eliminate the ambiguity of regional variable.

In addition to the name conflict it restricts the lifetime of the variable and that's good.

Parameters are local scope variables (I've seen language that this was not true, but is rare).

Regional

Variables of a class or structure can be considered local because the scope is limited, but not as much as it happens in the function, so some prefer to say that the scope is regional, the variable can be accessed by all methods of that type or possibly even out of it as long as it indicates that it is the variable of this type or object.

Block

The scope can be be less than the location . Most languages have command block scope, so a variable can have life only within the block and not throughout the function. Some languages do this in a confusing way. JavaScript is one of them, the scope only gets right when it uses let, with var it doesn't happen .

 2
Author: Maniero, 2020-06-11 14:45:34

Is basically a part, for example in the code below, I declared in the first line "var x = 1;" in the global scope, i.e. the scope of it is the complete part of the code, it is visible throughout the code, while "x" inside the function "scope" is only part of the scope of the function "scope" only.

To exemplify I created two functions and each function has its scope, in the first function I create a variable " x " only in the local scope of the function, that is, only in the function that exists that X, so when I call the scope function and show The " x "it shows the value of the global scope, now I created another function that has its own scope, only I did not create a new variable I used the" x " of the global scope and changed its value now when I show the value you notice that it was changed since I changed from the global scope and not from the local.

var x = 1; // Variável no escopo global
function escopo(){
  console.log("Chamou escopo");
  var x = 2; // Variável no escopo local
}
function escopo2(){
  console.log("Chamou escopo2");
  x = 2; // Variável no escopo global
}
escopo();
console.log(x);
escopo2();
console.log(x);

Knowing that the function has its own scope is possible to create functions without worrying if there is a variable in the global scope with the same name for example, so you have greater freedom in that scope to work.

 1
Author: Wictor Chaves, 2020-05-06 15:55:24