Credits Overview Plotting Styles Commands Terminals

Scope of variables

Gnuplot variables are global except in the special cases listed below. There is a single persistent list of active variables indexed by name. Assignment to a variable creates or replaces an entry in that list. The only way to remove a variable from that list is the undefine command.

Exception 1: The scope of the variable used in an iteration specifier is private to that iteration. You cannot permanently change the value of the iteration variable inside the iterated clause. If the iteration variable has a value prior to iteration, that value will be retained or restored at the end of the iteration. For example, the following commands will print 1 2 3 4 5 6 7 8 9 10 A.

     i = "A"
     do for [i=1:10] { print i; i=10; }
     print i

Exception 2: The parameter names used in defining a function are only placeholders for the actual values that will be provided when the function is called. For example, any current or future values of x and y are not relevant to the definition shown here, but A must exist as a global variable when the function is later evaluated:

     func(x,y) = A + (x+y)/2.

Exception 3: Variables declared with the local command. The local qualifier (new in version 5.5) allows optional declaration of a variable or array whose scope is limited to the execution of the code block in which it is found. This includes load and call operations, evaluation of a function block, and the code in curly brackets that follows an if, else, do for, or while condition. If the name of a local variable duplicates the name of a global variable, the global variable is shadowed until exit from the local scope.