User manual

234
mikoC PRO for dsPIC
MikroElektronika
Function Prototypes
A function can be dened only once in the program, but can be declared several times, assuming that the declarations
are compatible. When declaring a function, the formal argument’s identier does not have to be specied, but its type
does.
This kind of declaration, commonly known as the function prototype, allows better control over argument number, type
checking and type conversions. The name of a parameter in function prototype has its scope limited to the prototype.
This allows one parameter identier to have different name in different declarations of the same function:
/* Here are two prototypes of the same function: */
int test(const char*) /* declares function test */
int test(const char*p) /* declares the same function test */
Function prototypes are very useful in documenting code. For example, the function Cf_Init takes two parameters:
Control Port and Data Port. The question is, which is which? The function prototype:
void Cf_Init(char *ctrlport, char *dataport);
makes it clear. If a header le contains function prototypes, the user can read that le to get the information needed
for writing programs that call these functions. If a prototype parameter includes an identier, then the indentier is only
used for error checking.
Function Denition
Function denition consists of its declaration and function body. The function body is technically a block a
sequence of local denitions and statements enclosed within braces {}. All variables declared within function body are
local to the function, i.e. they have function scope.
The function itself can be dened only within the le scope, which means that function declarations cannot be nested.
To return the function result, use the return statement. The statement return in functions of the void type cannot have
a parameter – in fact, the return statement can be omitted altogether if it is the last statement in the function body.
Here is a sample function denition:
/* function max returns greater one of its 2 arguments: */
int max(int x, int y) {
return (x>=y) ? x : y;
}