AKD BASIC User Guide | 3 AKDBASICLanguage
The variable ‘i’ defined above in the ‘dim’ statement is a local variable — it is not accessible to
other functions, and inside ‘main’, its definition overrides any other variable named ‘i’ that might
exist at global scope.
Unlike global variables, local variables MUST be defined at the beginning of the section — they
must appear before any executable statement in main. For example, the following is illegal:
Main
dim i as integer
i = 1
dim j as integer ‘this is an error!
j = i
End Main
You may also define local constant definitions and aliases, provided that like local variables,
they appear before any executable statement. Local constant definitions override global def-
initions of the same name. For example, given the following global definitions,
const N = 1
Main
const N = “Hello, world!”
print N
call sub1
End Main
'-------------- Subroutines and Functions -----
-------
sub sub1
print N
end sub
The program prints to the console:
Hello world!
1
Because the N visible inside main is the constant defined there, while the N visible to sub1 is
the global constant N, whose value is 1.
The main program is the section of your program that is executed immediately after the‘params
section, regardless of its position in the program text. Other functions, subroutines, and inter-
rupt handlers are executed according to the flow of control defined in the program.
main does not accept arguments, and cannot be called from any other subroutine, function, or
interrupt handler.
3.3.2 Subroutine Definition
For a subroutine such as print_sum, a typical definition is:
sub print_sum(i,j as integer)
print i+j
end sub
The arguments to this subroutine are specified as integer variables, and are passed by value -
any assignments to these variables has no effect on the arguments supplied by the caller. Sub-
routines are invoked by ‘call’ instructions, as in call print_sum(3,4).
Kollmorgen™ | March 30, 2012 24