shared - declare variables as shared


shared Var[, Var]. . . 


The shared statement is used within a subprogram to gain access to variables outside the subprogram.  Var is the name of a simple variable or the name of an array followed by an empty pair of parentheses (without subscripts). 

By default, a variable that appears within a subprogram is different from a variable with the same name and type outside that subprogram.  That is, the scope of a variable within a subprogram is limited to that subprogram.  If a variable appears in a shared statement, then for the remainder of the subprogram, the variable is the same as if it appeared outside the subprogram.  That is, the shared statement can be used to prevent the scope limitation imposed on variables that appear in a subprogram, for particular variables and for a particular subprogram. 

The scope limitation imposed on variables takes place when the variable first appears and lasts for the duration of the subprogram, the limitation can not be undone.  If a variable is to be declared as shared, the variable may not appear earlier in the subprogram for any other purpose, including as a formal parameter, lest its scope be irrevocably narrowed.  On the other hand, a variable declared as shared may subsequently be redeclared as static (see static) at which point it will become a new and distinct variable. 

The use of the word shared in a common, dim or redim statement works just like a plain shared statement in each of the subprograms which follow it in that module.  Such a declaration obviates any explicit shared statements for those variables in the effected subprograms.  A shared statement may appear in a user defined function but accomplishes nothing since such a function does not impose the implicit scope limitations of a subprogram. 


The subprogram
	sub usrpoly(x) static
		shared order, coefficient()
		y = 0
		for i = order to 0 step -1
			y =  y * x + coefficient(i)
		next i
		usrpoly = y
	end sub
computes a polynomial the coefficientís of which are stored in the array "coefficient" and the order of which is stored in "order".  Since these variables are intended to be taken from the main body of the program, they are declared shared so that they are available to the subprogram.  On the other hand, "y" is not declared shared because we have no interest in a variable "y" outside the subprogram and in fact, wish to avoid clobbering such a variable. 


common, dim, redim, static, sub, usr

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber