Is there a common name for variables which are not local and not global?

49 views Asked by At

According to Wikipedia

a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program

What do we call a variable which is not local to any function but not accessible throughout the program, only to the functions declared in the same unit as the variable, where a unit can be a file, module or a class depending on the language?

Edit: Examples would be a static variable declared at file scope in C, a private static variable in Java or a non-exported variable with module scope in Oberon.

2

There are 2 answers

0
Bergi On

If the scope of the variable is not known, it is called a free variable.

Otherwise the non-local variables are typically named by the scope where they were declared, i.e. "global variable", "module(-scoped) variable", "file-scoped variable", "class(-scoped) variable" or whatever else units your language has.

0
JeremyP On

Is there a common name for variables which are not local and not global?

From my experience, I'd say "probably not".

In C, a static variable which is not declared extern is visible only in the same compilation unit. Because C compilation units hare files, we say it has "file scope".

In Swift, the equivalent to C file scope is "file private" (using the fileprivate key word). But, in Swift there's another level between the file and "global" scope. A variable can be declared internal which means accessible to anything else in the same module.

Other languages will have different terminology and different rules. For example, Java Has one class per file, so anything in the class (except function local variables) is visible to anything else in the same file. However, you can also have public variables and functions that are visible to anything in the program and also (I think - it's been a long time) package level visibility.