Variables

Variable is an object whose value can be changed during the runtime. Every variable is declared under unique name which must be a valid identifier. This name is used for accessing the memory location occupied by a variable.

Variables are declared in the declaration part of the file or routine — each variable needs to be declared before being used. Global variables (those that do not belong to any enclosing block) are declared below the include statements, above the label main.

Specifying a data type for each variable is mandatory. Syntax for variable declaration is:

dim identifier_list as type

Here, identifier_list is a comma-delimited list of valid identifiers, and type can be any data type.

For more details refer to Types and Types Conversions. For more information on variables’ scope refer to the chapter Scope and Visibility.

Here are a few examples:

dim i, j, k as byte
dim counter, temp as word
dim samples as longint[100]

External Modifier

Use the external modifier to indicate that the actual place and initial value of the variable, sub function or sub procedure body, is defined in a separate source code module.

For example, lets create a project which will calculate circle area and will have sub function and sub procedure definition in two different modules, and a call to these routines in the third, separate module.
So, the project will be consisted of the main module, Main_Module.mpas and First_Module.mpas and Second_Module.mpas modules.

In the Main_Module we will define routine called r_squared (calculates radius squared). Also, both modules must be included in the Main_Module :

program Main_Module

include First_Module
include Second_Module  ' Include both used modules

sub function r_square(dim r as float) as float  ' Definition of the r_square routine
  result = r*r;
end sub

main:
  CircleArea()  ' CircleArea routine call
end.
end.

In the First_Module we will define and declare routine called pi_r_squared (calculates pi multiplied by the radius squared):

module First_Module

sub procedure pi_r_square(dim rr as float)  ' Declaration of the pi_r_square routine

implements

sub procedure pi_r_square(dim rr as float)  ' Definition of the pi_r_square routine
  dim res as float
  res = rr*3.14
end sub

end.

In the Second_Module we will make a call to the routines defined externally (r_squared and pi_r_squared). First of all, we must declare their prototypes followed with a external modifier. Then, we can proceed to the routine call :

module Second_Module

sub procedure CircleArea()
sub function r_square(dim r as float) as float external  ' Declaration of the r_square routine (defined in Main_Module) followed with a external modifier
sub procedure pi_r_square(dim rr as float) external      ' Declaration of the pi_r_square routine (defined in Second_Module) followed with a external modifier

implements

sub procedure CircleArea()  ' Definition of the CircleArea routine
  dim res as real
  res = r_square(5)  ' Calculate r*r
  pi_r_square(res)   ' Calculate pi*r*r
end sub

end.

Variables and ARM

Every declared variable consumes part of RAM memory. Data type of variable determines not only the allowed range of values, but also the space a variable occupies in RAM memory. Bear in mind that operations using different types of variables take different time to be completed. mikroBasic PRO for ARM recycles local variable memory space – local variables declared in different functions and procedures share the same memory space, if possible.

There is no need to declare SFRs explicitly, as mikroBasic PRO for ARM automatically declares relevant registers as global variables of word. For example: W0, TMR1, etc.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code