Advanced PIC Microcontroller Projects in C: From USB to RTOS with the PIC18F Series

Chapter 4: Functions and Libraries in mikroC

4.1 mikroC Functions

A function is a self-contained section of code written to perform a specifically defined action. Functions are usually created when a single operation must be performed in different parts of the main program. It is, moreover, good programming practice to divide a large program into a number of smaller, independent functions. The statements within a function are executed by calling (or invoking) the function.

The general syntax of a function definition is shown in Figure 4.1. The data type indicates the type of data returned by the function. This is followed by the name of the function and then a set of parentheses, within which the arguments, separated by commas, are declared. The body of the function, which includes the function's operational code, is written inside a set of curly brackets.

    type name (parameter1, parameter,.....)    {          .........          function body          .........    }

Figure 4.1: General syntax of a function definition

In the sample function definition that follows, the function, named Mult, receives two integer arguments, a and b, and returns their product. Note that using parentheses in a return statement is optional:

<b class="bold">int</b> Mult(int a, int b){       return (a*b);}

When a function is called, it generally expects to be given the number of...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Food Additives
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.