2.03 Data Scope

Local Variables

The keyword Var is used to define a variable. Normally this will be local to the procedure or function where it is defined.

A variable that is declared and used only within a procedure or within the root segment is local to that procedure and is not the same as any other variable of the same name used elsewhere in the source module. Alternatively, a variable in the root segment may be declared as shared and be available throughout the source module. Within a procedure or function, where a local variable has the same name as a shared variable, the local variable will be used in preference.

The value of a local variable within a procedure or function is reset each time the procedure or function is called.

A variable that is defined within the root segment is static for the duration of the program.

The keyword local is currently supported as a synonym or as an alternative for var. This is deprecated and will be dropped in a future release. It only applies to local variables within a procedure or function. If used in the root segment it will be ignored. It will not cause an error.

There are various keywords that can be used to modify the scope of a data object.

Static

A variable that is declared as static is also local to the procedure. Unlike a local variable its value does not change across calls to the function or procedure.

When the procedure or function is called a second or subsequent time, the value of a static variable will be the same as it was left on the previous call.

Description:

Defines a variable where the value is kept across calls to the function or procedure and optionally assigns a value.

Syntax:

static [[as] type] name [= expr] [, name [= expr]] …
static name [= expr] [, name [= expr]] … [[as] type]

The syntax rules are the same as for the var command.

Notes:

The keyword static only applies to local variables within a procedure or function. If used in the root segment it will be ignored. It will not cause an error.

Shared

A variable that is declared as shared in the root segment of a source module is available to all functions and procedures at all levels within the same source module.

Description:

Defines a variable as shared between all functions and procedures and at all levels of the program and optionally assigns a value.

Syntax:

shared [[as] type] name [= expr] [, name [= expr]] …
shared name [= expr] [, name [= expr]] … [[as] type]

The syntax rules are the same as for the var command.

Notes:

Where a variable is declared as both local and shared either explicitly or implicitly, it will be treated as local. Unless a variable is explicitly declared as local within a procedure, all references to that variable are taken to refer the shared variable of that name.

proc myproc
  local lv;      // local variable
  lv = 2
  put lv;        // displays 2
  end proc //myproc

shared lv = 1;   // global variable
myproc
put lv;          // displays 1

External

Where an object is declared as external it may or may not be defined within the current source module. It must be defined once somewhere before use in any of the modules that reference it.

Within a source module the external variable can be accessed within any function or procedure in the same way as a shared variable.

Description:

Defines a variable as external to this module, shared between all functions and procedures and at all levels of the program and optionally assigns a value.

Syntax:

external name, name
external [[as] type] name [= expr] [, name [= expr]] …
external name [= expr] [, name [= expr]] … [[as] type]

The first form is used where the variable is defined in a separate source module.

The other forms are used to both declare an external variable for use by other modules and also to define it within the current source module. In this case the syntax rules are the same as for the var command.

Notes:

Where a variable is declared as external it is accessible by separately compiled modules, otherwise the usage rules are identical to shared.

Public/Private

A procedure or a function may be declared as either public or private. By default a procedure or function is public. It may be seen and accessed from any other source module. By declaring it as private, it may only be accessed from within the source module in which it is defined.

Similarly variables and constants defined in the root segment may also be declared as public or private. By default they are private, but by declaring them as public they may be accessed from another module. The data object must be declared as external within the other source module.

Description:

Defines a procedure, function or data object as local to this source module or as accessible from another source module.

Syntax:

[[as] type] name [= expr] [, name [= expr]] … public
name [= expr] [, name [= expr]] … [[as] type] public
private function …

Notes:

Common

If a variable is declared as common, the same variable is available at all levels of the program and across all modules. The use of common is dependent on the linker that is being used. If the linker does not support common storage, then common is effectively the same as shared.

Description:

Defines a variable as common across all modules. It will also be shared between all functions and procedures and at all levels of the program.

A matching common statement must appear in all other code modules using the variable.

Syntax:

common [as] type name [, …]
common name [as] type [, …]

The syntax rules for name and type are the same as for the var command.

Notes:

Common arrays are always variable-length, and must be defined with an empty parameter list (), and its dimensions set in a later Dim or ReDim statement.

Example:
common1.pls

declare proc initme()
common thingy() as double
dim thingy(0 to 2) as double
initme()
put thingy(0), thingy(1), thingy(2)

common2.pls

common thingy() as double
proc initme()
  thingy(0) = 4*Atn(1)
  thingy(1) = thingy(0)/3
  thingy(2) = thingy(1)*2
  end proc

Output:

 3.141592653589793           1.047197551196598           2.094395102393195

Implementation Differences:

LynPlex Interpreter
local is used instead of var.
local, static and shared are supported but without the option to assign a value.
As variables within the interpreter do not have a fixed type the type option is not supported.
LynPlexC Compiler
All commands are supported but with slight differences in the syntax rules.
LynPlexS Compiler
static and shared are supported but without the option to assign a value.
auto is used instead of var.

See Also


lynplex/lp0203.txt · Last modified: 2013/05/29 19:59 (external edit)