|
Global | |||
Global StringOrValue | |||
Parameters: StringOrValue = The Integer,FLoat are String that you wish to declare as global |
|||
Returns: NONE | |||
Global declarations affect the visibility of variables throughout a program. By default, variables are only visible within the scope their defined or used within. So variables used within the program main are not visible from within Function or PSub blocks. Likewise, any variables created within your functions / subs are equally only visible within the scope (function/sub) their declared. This is referred to as encapsulation, as variables are encapsulated within the scope their defined. This protects variables from being harmed by a different scope accidentally. However this is not always convenient, as some times we need certain variables to be visible from every place within a program. T his is where global variables step in. What the Global directive does, is define variables that are visible from not only within the main program, but from within functions/sub's also. Global supports two basic forms, You can simply define a single global or list of globals using comas to separate them, or define a global with an assignment. i.e
Note: Variables defined as global, with have precedence over any variable of the same name used within a function or PSub. You can override this, on a case by case basis if need be, via using the Local directive. FACTS: * Global can handle assignments. I.e. (Global Score=1000) * Global can handle lists by using coma's between them. I.e. (Global Monday,Tuesay,Wednesday,Thursday,Friday) * Global Variables have precedence over any variable of the same name used within functions or psubs. This can be overridden by using the local declaration. Mini Tutorial: This simple examples declares two variables. One is defined as global and the other is local by default. The example then displays these two variables from within a function.
In the output, You'll notice that only the global variable is visible from within the function. While the variable "MYLocalVariable" returns a zero. This occurs, as the while it may appear that "MyLocalVariable" is the same variable as the one defined in the main program, in fact it's not. Functions/Psub have their own sets of local variables completely. So while the MyLocalVaribale variable name is used within the main program and the function, they are two entirely separate variables. Who share a name, but nothing else.
|
Related Info: | Constant | Dim | Explicit | Function | Functions&Psub | Local | Psub | Static | Variables : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |