local

Prev Next

Function Names

local

Synopsis

local { statements; }
local () statement;
local () { statements; }

Description

In the statement or code block following the local function, all new variables defined are local variables as long this function is not called from a main program with global variables scope, i.e. outside code in user-defined functions and progams not started with start() or include(). In this case, global variables are defined instead. In other words, local variables will always be created, even if global variables exist with same names.

Note: The rules are not applicable for system variables. Insie the local code block, system variables can be read, written and member variables of them can be created. Only write-access to global variables are prevented.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

0

Examples

  country[] = France;

  define procedure( do locally )
  {
      echo("    Inside 'do locally' function before assignment: ", country[] );
      local() country[] = Spain;                // Create a local variable, do not overwrite global variable with same name
      local() runtime settings[epsilon] = 0.1;  // This is a system variable
      echo("    Inside 'do locally' function after  assignment: ", country[], new line );
  }

  define procedure( do globally )
  {
      echo("    Inside 'do globally' function before assignment: ", country[] );
      country[] = Portugal; // Create a local variable, do not overwrite global variable with same name
      echo("    Inside 'do globally' function after  assignment: ", country[], new line );
  }

  echo("Outside function: ", country[] );
  do locally;
  echo("Outside function: ", country[] );
  do globally;
  echo("Outside function: ", country[] );


  // Verify that system variables can be changed even in local-encorced code.
  echo("Runtime settings - Epsilon: ", runtime settings[epsilon] ); // Value set to 0.1 in 'do locally()'

Output

Outside function: France
    Inside 'do locally' function before assignment: France
    Inside 'do locally' function after  assignment: Spain

Outside function: France
    Inside 'do globally' function before assignment: France
    Inside 'do globally' function after  assignment: Portugal

Outside function: Portugal
Runtime settings - Epsilon: 0.1
Try it yourself: Open LIB_Function_local.b4p in B4P_Examples.zip. Decompress before use.

See also

global
regional