Mikroelektronika MIKROE-350 データシート

ページ / 526
Example
Here’s a simple function which calculates x
n
based on input parameters 
x
and 
n
(
n > 0
):
sub function power(dim x, n as byte) as longint
dim 
as byte
result = 1
if n > 0 then
for i = 1 to n
result = result*x
next i
end if
end sub
Now we could call it to calculate, say, 312:
tmp = power(3, 12)
PROCEDURES
Procedure is declared like this:
sub procedure procedure_name(parameter_list)
[ local declarations ]
procedure body
end sub
procedure_name
represents a procedure’s name and can be any valid identifier. Within
parentheses, 
parameter_list
is a formal parameter list similar to variable declaration. In
mikroBasic PRO for AVR, parameters are always passed to procedure by value; to pass
argument by address, add the keyword 
byref
ahead of identifier.
Local declarations 
are optional declaration of variables and/or constants, local
for the given procedure. 
Procedure
body is a sequence of statements to be execut-
ed upon calling the procedure.
Calling a procedure
A procedure is called by its name, with actual arguments placed in the same
sequence as their matching formal parameters. The compiler is able to coerce mis-
matching arguments to the proper type according to implicit conversion rules. Upon
procedure call, all formal parameters are created as local objects initialized by val-
ues of actual arguments.
Procedure call is a self-contained statement.
132
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroBasic PRO for AVR
CHAPTER 5