Function definition, Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 105

Advertising
background image

Function Definition

Function definition consists of its declaration and a function body. The function
body is technically a block – a sequence of local definitions 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 defined only within the file scope. This means that func-
tion declarations cannot be nested.

To return the function result, use the

return

statement. Statement

return

in

functions of

void

type cannot have a parameter – in fact, you can omit the

return

statement altogether if it is the last statement in the function body.

Here is a sample function definition:

/* function max returns greater one of its 2 arguments: */

int

max(int x, int y) {

return

(x>=y) ? x : y;

}

Here is a sample function which depends on side effects rather than return value:

/* function converts Descartes coordinates (x,y)

to polar coordinates (r,fi): */

#include <math.h>

void

polar(double x, double y, double *r, double *fi) {

*r = sqrt(x * x + y * y);

*fi = (x == 0 && y == 0) ? 0 : atan2(y, x);

return

;

/* this line can be omitted */

}

MikroElektronika: Development tools - Books - Compilers

97

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: