Free Variable

Note: The following is not a definition of actual Free Variables as seen in mathematics, but that of top level lexically scoped variables.

A free variable is a variable used within a function, which is neither a formal parameter to the function nor defined in the function's body (and in scope at the point of the variable's use).

For example, in this C function, printf and phase_shift are free variables. The first is a standard library function; the second had better be defined by the programmer; else this function is in error. angle, being a formal parameter, is not a free variable; nor is x. The first use of sin is a Free Variable; it too is a standard library function (one which returns the sine of the angle). The second use of sin (in the if statement) is not, as a local definition for sin is provided (which shadows the library function).

We could argue that "double", "if", and "int" are Free Variables as well; but those are keywords so we'll ignore them.

void silly (double angle) { double x = sin (angle + phase_shift); printf ("The sine of the angle, phase shifted, is %f\n", x); if (angle > 0) { int sin = 7; printf ("There are %d deadly sins, %d if you count " "pointer arithmetic\n", sin, sin+1); } }

Programming languages handle free variables via one of the Scoping Rules.

See original on c2.com