3.3 Functions in mikroC Pro for PIC

In general, it is a good programming practise to write a large complex program as a collection of smaller modules, where each module can be tested independent of the main code. A function can be thought of as a self-contained, testable program code, developed to perform a specific task. Functions are also created when it is required to repeat a certain algorithm at several different places of the main program. For example, it may be required to convert the temperature from °F into °C at several places in a program. Instead of repeating the code, it is more efficient and the program is more maintainable if a temperature conversion code is written in the form of a function. This function can then be called whenever it is required to make the required conversion.

The general format of a function declaration is as follows:

img

Functions usually (not always) perform a certain operation and return data to the calling program. The function type indicates the type of returned data, name is the name of the function, and the parameters (if any) should be separated by commas. The statements inside the function should be enclosed within a pair of curly brackets.

An example function declaration is given below, which calculates the circumference of a circle and returns to the calling program. Here, the radius of the circle is passed as an argument to the function: ...

Get Using LEDs, LCDs and GLCDs in Microcontroller Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.