User manual
234
mikoC PRO for dsPIC
MikroElektronika
Function Prototypes
A function can be dened 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 identier does not have to be specied, 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 identier 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 identier, then the indentier is only
used for error checking.
Function Denition
Function denition consists of its declaration and function body. The function body is technically a block – a
sequence of local denitions 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 dened 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 denition:
/* function max returns greater one of its 2 arguments: */
int max(int x, int y) {
return (x>=y) ? x : y;
}