Regional Variables

Prev Next

Introduction

Local variables created inside user-defined procedures and user-defined functions, as well as in B4P programs can be made visible as regional variable in the code of user-defined functions called from here. This approach is useful if you want to provide numerous variables to the function, but don't want to create processing overhead for passing them as function parameters, nor using global variables as other side effects cannot be avoided.

Demonstrating regional variables
       define procedure( zoo )
       {
           echo('Zoo in ', city[], ' ', country[]);
           city[] = Cambridge;                  // city and country are regional variables
           country[] = United Kingdom;
           echo( "   Scope of country[] in 'zoo': ", scope(country[]), " and city[]: ", scope( city[]) );
       }

       define procedure( bar )
       {
           country[] = USA;                     // country is a local variable, but city is not
           echo('Bar in ', city[], ' ', country[]);

           regional  // Alternative formulation
           {
               zoo;
           }
           echo('Bar in ', city[], ' ', country[]); // Cambridge, UK
           echo( "   Scope of country[] in 'bar': ", scope(country[]), " and city[]: ", scope( city[]) );

       }

       define procedure( foo )
       {
           city[] = Boston;
           regional() bar;
           echo('Foo(d) in ', city[]); // Cambridge
       }

       // Main program
       foo;
Bar in Boston USA
Zoo in Boston USA
   Scope of country[] in 'zoo': regional and city[]: regional
Bar in Cambridge United Kingdom
   Scope of country[] in 'bar': local and city[]: regional
Foo(d) in Cambridge
Try it yourself: Open LAN_Features_Regional_variables.b4p in B4P_Examples.zip. Decompress before use.

See also

regional