Futurebasic/Language/Reference/begin globals
Begin Globals
editStatement
edit✔ Appearance ✔ Standard ✔ Console
Syntax
editbegin globals [statements including variable declarations] end globals
Description
editThe begin globals
and end globals
statements indicate the beginning and end, respectively, of a section of global variable declarations. A global variable is one which is "visible" to all parts of the program: that is, it maintains its value when local function are entered or exited. By judicious placement of begin globals...end globals
statements, you can also create variables which are considered "global" to some local functions but not to others.
begin globals
and end globals
are "non-executable" statements, so you can't change their effect by putting them inside a conditional execution structure such as long if...end if
. However, you can conditionally include or exclude them from the program by putting them inside a compile long if
block.
You may include any number of begin globals...end globals
pairs in your program, although typically global variables are all defined within a single section near the beginning of the program. You may also include begin globals...end globals
pairs in local functions. They must occur in matched pairs when they occur within a local function, and should normally be in matched pairs when they occur in the "main" part of your program (the "main" part consists of those lines which are outside of all local functions). When you include a begin globals...end globals
section in "main," it should not enclose any local functions, or variables may be scoped in unpredictable ways.
When a variable's first appearance within "main" occurs within a begin globals...end globals
section, that variable is declared as global to all local functions which appear below that section. All other variables in "main" are local to "main." Important: FB places an "implicit" begin globals
statement at the beginning of your program. That means that, by default, all variables declared in "main" are global. You must include an end globals
statement in "main" if you want any of the variables declared in "main" to be local to "main."
When a variable's first appearance within a local function occurs within a begin globals...end globals
section, that variable is declared as global to that function and to all local functions which appear below that function. That variable is also global in "main," if its first appearance in "main" occurs below the function in which the variable was declared global. All other variables in the local function are local to that function, unless they were declared global in some preceding begin globals...end globals
section.
Notes
editWith respect to global variables, the default behavior of FB is different from that of FutureBasic II. If you include neither a begin globals
nor an end globals
statement anywhere in your program, then the two versions behave as follows:
FutureBasic II: All variables declared in "main" are local to "main."
Modern FB: All variables declared in "main" are global.
If you have a FutureBasic II program which does not contain an end globals
statement, you should add an end globals
statement to the beginning of the code (before any variables are declared) in order to make it run as expected in FB. If your FutureBasic II program already has an end globals
statement in it, you should not change it.