Local Variables

Prev Next

Introduction

Variables 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, will be local variables. Local variables are only visible locally, i.e.

  • Inside the presently running user-defined procedure or function
  • Inside a B4P program started with start() or include() from a different B4P program (or the same one because recursive calls are allowed).

The local variables are not visible if a further procedure, function or B4P program is called.

Variables may be deleted using the delete() function. Local variables will be deleted automatically when leaving (returning from) user-defined procedures and functions or called programs.

Demonstrating global and local variables
  define procedure( foo, { { a, numeral } } )
  {
      if (a[] < 4) // If a[] exists a local variable, then the local variable will be used preferentially.
      {
          foo( 4 ); // Recursive call
          a[] = 5;
          echo( a[] ); // Called 3rd: 5
      }

      echo( a[] ); // Called 1st and 4th: 4
      a[] = 3;
      echo( a[] ); // Called 2nd and 5th: 3
  }

  a[] = 1;
  foo( 2 );
  echo(a[] ); // Called 6th: 1 (Global variable)
4
3
5
5
3
1
Try it yourself: Open LAN_Features_Local_variables.b4p in B4P_Examples.zip. Decompress before use.

See also

local