Scopes

There are two types of scopes: global and local.


The Global Scope

Every source file (.TBS or .TC), as well as every HTML file (.HTML), with Tibbo BASIC or C code in it contains within itself one global scope.

A variable defined outside of any procedure is considered to be within this global scope and is called a global variable.

A global variable of one source file cannot be used in another source file unless this variable is declared there:

Tibbo BASIC
Tibbo C
declare dw as dword 'defined in some other file
extern unsigned long ul; //defined in some other file

Such declarations are usually placed in header files.TPH for Tibbo BASIC and .TH for Tibbo C. Header files must be included into your source code file:

Tibbo BASIC
Tibbo C
include "global.tbh"
#include "global.th"

Despite what's just been said, global variables must have unique names throughout your project. Defining two global variables with the same name in two different files will generate a linker error ... with the following exception:

In Tibbo C, you can define multiple static global variables with the same name in different files. The same is not possible in Tibbo BASIC, because it doesn't support the static storage class specifier.


A warning note icon.There is no cross-checking of variable types between files. You could define f as a word type in one file and then declare it as a string type in another file and TIDE would not notice! Be very careful with this!


Local Scopes

Every procedure of BASIC and C has its own local scope.

The special feature of Tibbo BASIC and Tibbo C is that in addition there are even smaller local (and nesting) scopes within control structures.

Think of it this way: anywhere execution forks (or loops), there are as many sub-scopes as there are possible paths the execution can take.

Local variables and other entities can be defined on any scope's level!


A local variable (or other entity) takes precedence over all variables (as well as procedure names and other entities) with the same name that are defined at outer (higher) scopes.

Here is an example of code that declares a variable f in every possible scope. There are seven of them here!

Tibbo C
unsigned int f;            //1: global scope of this source file

void on_sys_timer(){
   unsigned char f;        //2: local scope of on_sys_timer(){}
   f=1;
lbl1:   if(f==1){
      unsigned int f;      //3: local sub scope inside else{}
      switch(f){
      case 0:
         bool f;           //4: local sub-sub scope inside "case"
         break;
      case 1:
         unsigned long f;  //5: local sub-sub scope inside "case"
         break;
      }
   }
   else{
      char f;              //6: local sub scope inside else{}
      for(f=0;f<11;f++){
         string f;         //7: local sub-sub scope inside for(;;)
         f="ABC";
      }
   }
   f=0;                    //continued -- local scope of on_sys_timer(){}
   goto lbl1;         
}
//continued -- global scope

Scopes in HTML

The peculiarity of HTML files is that BASIC/C code inside is not arranged into procedures. The entire HTML file is like one large procedure.

Therefore, procedure-level scope doesn't exist in HTML files. Control structures still have sub-scopes.

Here is one HTML example (the rules of putting code in HTML files are explained in Working with HTML):

HTML with Tibbo BASIC
<HTML>
<BODY>
BEGINNING OF OUTPUT<br>
   
<?
dim i as integer         '1: global scope of this source file
for i = 1 to 10
   dim i as byte=2       'local scope inside for-to 
   const TIMER_CONST=10  'this const is only visible in this for-to scope
?>

<i> Foo </i>

<?
next i                   'continued -- global scope
?>
      
<br>END OF OUTPUT<br>
</BODY>
</HTML>

Scopes

The Global Scope

Local Scopes

Scopes in HTML