# Free variable in the programming language

From the wikipedia of Free variables and bound variables

In computer programming, the term free variable refers to variables used in a function that are neither local variables nor parameters of that function. The term non-local variable is often a synonym in this context.

Since local variables and parameters are bound to particular invocation of the function. Can we say only global variables are the free variables?

• The free variables of a function could be bound in an enclosing scope, e.g. a closure. Commented Jan 8 at 8:30
• @Pseudonym "enclosing scope" keyword is infact use in the answer by Jorg. I am confused on what closure have to do with free variable. Looking for convincing rationale on this. Commented Jan 9 at 9:41
• @tnhaxor See, for example, this: opendsa.cs.vt.edu/ODSA/Books/PL/html/FreeBoundVariables.html Commented Jan 9 at 13:51

Can we say only global variables are the free variables?

No.

function outerFn() {
const outerVar = "Outer";

function innerFn() {
return outerVar;
}

// Could also be written:
const innerFn2 = () => outerVar;

return innerFn();
}

console.log(outerFn());

def outer_fn():
outer_var = "Outer"

def inner_fn():
return outer_var

return inner_fn()

print(outer_fn())

def outer_method
outer_var = "Outer"

inner_lambda = -> () { outer_var }

inner_lambda.()
end

puts outer_method

def outerMethod =
val outerVar = "Outer"

def innerFn = outerVar

innerFn

println(outerMethod)

public class FreeVariablesInJava {
static String outerMethod() {
var outerVar = "Outer";

java.util.function.Supplier<String> innerFn = () -> outerVar;

return innerFn.get();
}

public static void main(String args[]) {
System.out.println(outerMethod());
}
}

• Why only closure, foo = lambda: bar is also a valid function. Though it will give reference error and that's the one of the rules of programming (not to be thought of computer science), all variables are bound one way or another. Commented Jan 9 at 9:50