User manual
198
mikoBasic PRO for PIC32
MikroElektronika
Function calls are considered to be primary expressions and can be used in situations where expression is expected.
A function call can also be a self-contained statement and in that case the return value is discarded.
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 i 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, 3
12
:
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 identier. Within parentheses, parameter_
list is a formal parameter list similar to variable declaration. In mikroBasic PRO for PIC32, parameters are always
passed to procedure by value; to pass argument by address, add the keyword byref ahead of identier.
Local declarations are optional declaration of variables and/or constants, local for the given procedure. Procedure
body is a sequence of statements to be executed 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 mismatching arguments to the proper type according to implicit conversion
rules. Upon procedure call, all formal parameters are created as local objects initialized by the values of actual
arguments.
Procedure call is a self-contained statement.