float Area(float radius, float height) {
float s;
s = 2.0*PI * radius*height;
return s;
}
/* Function to calculate the volume of a cylinder */
float Volume(float radius, float height) {
float s;
s = PI *radius*radius*height;
return s;
}
/* Start of the main program */
void main() {
float r = 2.0, h = 5.0;
float cyl_area, cyl_volume;
cyl_area = Area(r, h);
cyl_volume(r, h);
}
Figure 4.4: Program that calculates the area and volume of a cylinder
Write a function called LowerToUpper to convert a lowercase character to uppercase.
The ASCII value of the first uppercase character (‘A’) is 0x41. Similarly, the ASCII value of the first lowercase character (‘a’) is 0x61. An uppercase character can be converted to its equivalent lowercase by subtracting 0x20 from the character. The required function listing is shown in Figure 4.5.
unsigned char LowerToUpper(unsigned char c) {
if (c >= 'a' && c <= 'z') return (c - 0x20);
else return c;
}
Figure 4.5: Function to convert lowercase to uppercase
Use the function you created in Example 4.3 in a main program to convert letter ‘r’ to uppercase.
The required program is shown in Figure 4.6. Function LowerToUpper is called to convert the lowercase character in variable Lc to uppercase and store in Uc.
/********************************************************************
LOWERCASE TO UPPERCASE
==========================
This program converts the lowercase character in variable Lc to uppercase
and stores in variable Uc.
Programmer: Dogan Ibrahim
File: LTOUPPER.C
Date: May, 2007
*********************************************************************/
/* Function to convert a lower case character to upper case */
unsigned char LowerToUpper(unsigned char c) {
if (c >= 'a' && c <= 'z') return (c - 0x20);
else return c;
}
/* Start of main program */
void main() {
unsigned char Lc, Uc;
Lc = 'r';
Uc = LowerToUpper(Lc);
}
Figure 4.6: Program calling function LowerToUpper
4.1.1 Function Prototypes
If a function is not defined before it is called, the compiler will generate an error message. One way around this problem is to create a function prototype. A function prototype is easily constructed by making a copy of the function’s header and appending a semicolon to it. If the function has parameters, giving names to these parameters is not compulsory, but the data type of the parameters must be defined. An example follows in which a function prototype called Area is declared and the function is expected to have a floating point type parameter:
float Area(float radius);
This function prototype could also be declared as:
float Area(float);
Function prototypes should be declared at the beginning of a program. Function definitions and function calls can then be made at any point in the program.
Repeat Example 4.4 but declare LowerToUpper as a function prototype.
Figure 4.7 shows the program where function LowerToUpper is declared as a function prototype at the beginning of the program. In this example, the actual function definition is written after the main program.
/**********************************************************************
LOWERCASE TO UPPERCASE
=========================
This program converts the lowercase character in variable Lc to uppercase
and stores in variable Uc.
Programmer: Dogan Ibrahim
File: LTOUPPER2.C
Date: May, 2007
************************************************************************/
unsigned char LowerToUpper(unsigned char);
/* Start of main program */
void main() {
unsigned char Lc, Uc;
Lc = 'r';
Uc = LowerToUpper(Lc);
}
/* Function to convert a lower case character to upper case */
unsigned char LowerToUpper(unsigned char c) {
if (c >= 'a' && c <= 'z') return (c - 0x20);
else return c;
}
Figure 4.7: Program using function prototype
One important advantage of using function prototypes is that if the function prototype does not match the actual function definition, mikroC will detect this and modify the data types in the function call to match the data types declared in the function prototype. Suppose we have the following code:
unsigned char c = 'A';
unsigned int x = 100;
long Tmp;
long MyFunc(long a, long b); // function prototype
void main() {
...............
...............
Tmp = MyFunc(c, x);
...............
...............
}
In this example, because the function prototype declares the two arguments as long, variables c and x are converted to long before they are used inside function MyFunc.
4.1.2 Passing Arrays to Functions
There are many applications where we may want to pass arrays to functions. Passing a single array element is straightforward, as we simply specify the index of the array element to be passed, as in the following function call which passes the second element (index = 1) of array A to function Calc. It is important to realize that an individual array element is passed by value (i.e., a copy of the array element is passed to the function):
x = Calc(A[1]);