NAME

static - declare variables as static

SYNOPSIS

static Var[, Var]. . . 

DESCRIPTION

The static statement is used within a subprogram or user defined function to limit the scope of variables to the subprogram or function.  Limited scope is the default for subprograms.  Var is the name of a simple variable or the name of a dynamic array followed by a single parentheses enclosed constant which represents the number of dimensions in the array. 

When a variable is declared as static within a subprogram or user defined function, it becomes a new and distinct variable for the duration of the subprogram or function.  So declared, the variable has scope limited to the subprogram or function in which the declaration appears.  The scope limitation imposed on variables can not be undone.  Even a formal parameter can be redeclared as static but thereafter retrieving the value of the parameter is impossible.  A non-dynamic (a.k.a.  static) array can not be declared static. 

Declaring a variable static is a wise means of ensuring that the name one chooses for a variable used only inside a subprogram or user defined function will not have mysterious side-effects on the rest of the program.  Although it is the default, a variable used in a subprogram ought to be explicitly declared static because statements outside the subprogram might declare a variable shared (see shared). 

EXAMPLE

The program
	def fnfact(n)
		static tot, i

		tot = 1
		for i = 1 to n
			tot = tot * i
		next i
		fnfact = tot
	end def

	for i = 1 to 10
		print using "#######"; fnfact(i)
	next i
prints one through 10 factorial:
	      1
	      2
	      6
	     24
	    120
	    720
	   5040
	  40320
	 362880
	3628800
Within the user defined function, the temporary variables are declared as static, which is not only good programming practice, but in the case of "i", a necessity lest the "i" outside the function be modified haphazardly by the "i" within. 

SEE ALSO

shared

from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber