Difference between global and local scope

Asked

Viewed 286 times

2

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

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

2

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

I’ve talked about scope in What is the difference between scope and lifetime?.

Global

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

They are usually dangerous and should be avoided. It’s not that you can’t do it, but you need to understand what you’re doing, to know that it can be confused with other things, it’s circulating around and visible everywhere. In addition to name conflicts can have competition problems when the application allows this.

Local

Local scope means the shuttle has limited scope. The most common is being a function, so it is only visible within that function and does not get 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, do not have the risk of change coming from all locations.

If you access a global variable locally it can get confused with a local. 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 to resolve ambiguity, but not always. It is more common to have a way to eliminate the ambiguity of regional variable.

Besides the conflict of names it restricts the life time of the variable and this is good.

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

Regional

Variables of a class or structure can be considered local because the scope is limited, but not as much as 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 outside of it since it indicates that it is the variable of this type or object.

Block

The scope may be smaller than the location. Most languages have a 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 is only right when you use the let, with the var that doesn’t happen.

1

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

To illustrate 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, then 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 didn’t create a new variable I used the "x" of the global scope and I changed its value now when I show the value you realize it was changed since I changed the global scope and not 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 it 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 a greater freedom in that scope to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.