User manual

Table Of Contents
230
mikoC PRO for PIC32
MikroElektronika
Functions
Functions are central to C programming. Functions are usually dened as subprograms which return a value based
on a number of input parameters. Return value of the function can be used in expressions – technically, function call is
considered to be an expression like any other.
C allows a function to create results other than its return value, referred to as side effects. Often, the function return value
is not used at all, depending on the side effects. These functions are equivalent to procedures of other programming
languages, such as Pascal. C does not distinguish between procedure and function – functions play both roles.
Each program must have a single external function named main marking the entry point of the program. Functions
are usually declared as prototypes in standard or user-supplied header les, or within program les. Functions have
external linkage by default and are normally accessible from any le in the program. This can be restricted by using the
static storage class specier in function declaration (see Storage Classes and Linkage).
Note: Check the PIC32 Specics for more information on functions’ limitations on the PIC32 MCUs.
Function Declaration
Functions are declared in user’s source les or made available by linking precompiled libraries. The declaration syntax
of the function is:
type function_name(parameter-declarator-list);
The function_name must be a valid identier. This name is used to call the function; see Function Calls for more
information.
type represents the type of function result, and can be of any standard or user-dened type. For functions that do not
return value the void type should be used. The type can be omitted in global function declarations, and function will
assume the int type by default.
Function type can also be a pointer. For example, oat* means that a function result is a pointer to oat. The generic
pointer void* is also allowed.
The function cannot return an array or another function.
Within parentheses, parameter-declarator-list is a list of formal arguments that function takes. These
declarators specify the type of each function parameter. The compiler uses this information to check validity of function
calls. If the list is empty, a function does not take any arguments. Also, if the list is void, a function also does not take
any arguments; note that this is the only case when void can be used as an argument’s type.
Unlike variable declaration, each argument in the list needs its own type specier and possible qualier const or
volatile.