The definition of scope.

Since my English isnt the best I would like to get the word Scope explained for me in a simple English language. I´ve been searching on google for the definition of Scope. But It´s complicated for me to understand what they mean with it. So please explain for me what Scope is. 

Thanks Josh.

What’s your native language?

In essence you have two types of scope:

  1. global scope. Global scope is more practical for some uses, because your variables are available everywhere in your app. But they are a little bit slower to use, although you probably won’t feel this in reality. However, because they are available everywhere in your app you can pollute you app this way.

If you declare a variable without using “local” it is always global

  1. local scope. Local scope is faster, and pollutes your code less, because local variable will stop to exist outside of your code-block. This can sometimes be harder to use because local variable might not be seen from or accessed from outside your code block.

By the way, a code block is e.g. a for loop, an if-branch, a module etc…

In code, “scope” refers to how much of your code can reference a particular variable.

@thomas6 is absolutely correct. As he says, there are two types of scope: local and global.

Generally, we talk about a variable’s scope as being the module, function or while, do, if or else statement that it is declared in. For example:

local name = "myModule" local function printSomething( text ) if (text == nil) then print("Nothing") lastprinted = "nothing" else local pre = "I want to print: " print( pre..text ) lastprinted = pre..text end end

The code above is very bad, but it illustrates this:

name is local to the Lua module and can be used by all code in this .lua file

text is local to the function and can be used by all the code in this function only

lastprinted is global and can be used by the whole application - even other Lua modules

pre  is local to only the code between the  else and the  end of the  if statement, not the whole function

Nice!! The last part explained things that I never came across. Thank you for the support.

What’s your native language?

In essence you have two types of scope:

  1. global scope. Global scope is more practical for some uses, because your variables are available everywhere in your app. But they are a little bit slower to use, although you probably won’t feel this in reality. However, because they are available everywhere in your app you can pollute you app this way.

If you declare a variable without using “local” it is always global

  1. local scope. Local scope is faster, and pollutes your code less, because local variable will stop to exist outside of your code-block. This can sometimes be harder to use because local variable might not be seen from or accessed from outside your code block.

By the way, a code block is e.g. a for loop, an if-branch, a module etc…

In code, “scope” refers to how much of your code can reference a particular variable.

@thomas6 is absolutely correct. As he says, there are two types of scope: local and global.

Generally, we talk about a variable’s scope as being the module, function or while, do, if or else statement that it is declared in. For example:

local name = "myModule" local function printSomething( text ) if (text == nil) then print("Nothing") lastprinted = "nothing" else local pre = "I want to print: " print( pre..text ) lastprinted = pre..text end end

The code above is very bad, but it illustrates this:

name is local to the Lua module and can be used by all code in this .lua file

text is local to the function and can be used by all the code in this function only

lastprinted is global and can be used by the whole application - even other Lua modules

pre  is local to only the code between the  else and the  end of the  if statement, not the whole function

Nice!! The last part explained things that I never came across. Thank you for the support.