Global Variables

Prev Next

Introduction

If the variable is created in the B4P program file started first, and this happens outside user-defined procedures and functions, then it will be a global variable. Global variables are visible during the entire program execution, even while running user-defined functions or other B4P programs using the start() or include() function.

Global variables can also be created inside user-defined procedures and user-defined functions, as well as in B4P programs which have been started from other B4P programs using start() or include() functions if they are put into a code block following the global() function.

Global varibles will exist until the B4P program has ended or the variable has been been deleted explicityl with the function delete().

Defining global variables inside user functions:
       g[] = 4;
       define procedure( foo )
       {
           global
           {
               a[] = 5;
           }
           global() b[] = 6; // Alternative formulation for 1 statement
           echo( g[] ); // Global variables are always visible.
       }

       foo;
       echo( a[] );
       echo( b[] );
4
5
6
Try it yourself: Open LAN_Features_Global_variables.b4p in B4P_Examples.zip. Decompress before use.

See also

global