Is there any difference between free variable and global variable? Or they are just synonyms? In which situations I should use one or another?
-
1$\begingroup$ Every global variable is also a free variable, but not the other way around. $\endgroup$– user742Commented Aug 30, 2013 at 15:13
-
$\begingroup$ Does this question really relate to programming languages? The term "free variable" is more common in logics. $\endgroup$– RaphaelCommented Sep 2, 2013 at 9:49
2 Answers
They don't play in the same category.
The notion of free variable is relative to a scope. If a variable is present in a term (i.e. a subprogram) and its scope is larger than this term, then the variable is said to be free in that term.
A global variable is one whose scope is the whole program, or the whole file, or the whole module, or whatever scope is called “global” in the programming language you're considering.
According to Wikipedia, Free variables and bound variables,
The term free variable refers to variables used in a function that are not local variables nor parameters of that function. The term non-local variable is often a synonym in this context.
Now Wikipedia, Non-local variable states:
In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can be neither in the local nor the global scope.
The last quote is from Wikipedia, Global variable:
In computer programming, a global variable is a variable that is accessible in every scope (unless shadowed).