Mikroelektronika MIKROE-350 Fiche De Données

Page de 526
FUNCTIONS AND PROCEDURES
Functions and procedures, collectively referred to as routines, are subprograms (self-con-
tained statement blocks) which perform a certain task based on a number of input param-
eters. When executed, a function returns value while procedure does not.
Functions
Function is declared like this:
sub function function_name(parameter_list) as return_type
[ local declarations ]
function body
end sub
function_name
represents a function’s name and can be any valid identifier.
return_type
is a type of return value and can be any simple type. Within parenthe-
ses, 
parameter_list
is a formal parameter list similar to variable declaration. In
mikroBasic PRO for AVR, parameters are always passed to a function by value. To
pass an argument by address, add the keyword 
byref
ahead of identifier.
Local declarations
are optional declarations of variables and/or constants, local
for the given function. 
Function body
is a sequence of statements to be executed
upon calling the function.
Calling a function
A function 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 a function
call, all formal parameters are created as local objects initialized by values of actu-
al arguments. Upon return from a function, a temporary object is created in the place
of the call and it is initialized by the value of the function result. This means that func-
tion call as an operand in complex expression is treated as the function result.
In standard Basic, a 
function_name
is automatically created local variable that can
be used for returning a value of a function. mikroBasic PRO for AVR also allows you
to use the automatically created local variable 
result
to assign the return value of
a function if you find function name to be too ponderous. If the return value of a func-
tion is not defined the compiler will report an error.
Function calls are considered to be primary expressions and can be used in situa-
tions where expression is expected. A function call can also be a self-contained
statement and in that case the return value is discarded.
131
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroBasic PRO for AVR
CHAPTER 5