LOCAL

<< Click to Display Table of Contents >>

Navigation:  Gekko User Manual > Gekko statements >

LOCAL

Previous pageReturn to chapter overviewNext page

The LOCAL statement is used to designate variable names that are to be located in the Local databank. Following a local x; statement, any subsequent use of x (without databank designation) will be understood as local:x.

 

After Gekko leaves the program file, function or procedure, these local variables do not live on, so the variables only live in the local context. Therefore, using LOCAL or local:= ... can be practical regarding temporary variables that are not intended to live on, polluting the databanks (such variables amound to so-called "side-effects" of the program/function/procedure).

 

You may use local<all>; to render all bankless variables local inside a function or procedure. This is useful regarding encapsulation: ensuring that a function only uses its own arguments and local variables (unless a variable is stated with an explicit databank reference). After a local<all>, you can still search for a bankless variable x outside of the Local databank by means of the special all: designation (for instance = all:x;).

 

See also the similar GLOBAL statement for global variables, and the BLOCK structure for temporary settings.

 

 


 

Syntax

 

local varnames;

local <all>;

 

 

varnames

Comma-separated list of variables

ALL

(Optional). With this option, all following (in the rest of the program/function/procedure) left-hand side variables without explicit databank designation are located in the Local databank. This may practical for functions/procedures where no temporary variables are supposed to exist after the function/procedure has been left. For a variable x that you would like to keep despite using a local<all>, you may use first:x or another bank designation to circumvent local<all>.

 

 


 

Example

 

local x, %y, #z;

 

After this, any use of x, %y, or #z (in the present program file, function or procedure) will be interpreted as local:x, local:%y, and local:#z, respectively. And after Gekko leaves the current context (program file, function or procedure), these local variables cease to exist.

 

The Local databank is searched first, if databank searching is active (that is, data- or mixed mode), cf. databank search.

 

You may use local<all> in functions/procedures:

 

procedure test;
  local <all>;
  %= 2;
  %= 3;
  first:%= %+ %y;
end;
test;
val?;

 

Only %z will exist (in the first-position databank) after the procedure has been left. Still, it is perhaps better to use a function to return the value.

 

 


 

Note

 

You are not forced to use the LOCAL keyword, when operating with local variables. Defining local:%per1 = 2010; first, and referring to local:%per1 later on is fine, too. In that sense, the LOCAL keyword is just for convenience, especially if %per1 is used several times.

 

Local variables survive READ, CLEAR, etc., but do only live in their local context (program file, function or procedure). Hence, they do no 'pollute' the first-position databank if this is later on written to file.

 

Note that the Local or Global databanks are always searchable, independent of MODE etc.

 


 

Related statements

 

GLOBAL