Выбрать главу
Example 4.9

Write a function named Swap to accept two integer arguments and then to swap the values of these arguments. Use this function in a main program to swap the values of two variables.

Solution 4.9

The required program listing is shown in Figure 4.11. Function Swap is defined as void since it does not return any value. It has two arguments, a and b, and in the function header two integer pointers are used to pass the addresses of these variables. Inside the function body, the value of an argument is accessed by inserting the “*” character in front of the argument. Inside the main program, the addresses of the variables are passed to the function using the “&” character in front of the variable names. At the end of the program, variables p and q are set to 20 and 10 respectively.

/*************************************************************

                PASSING VARIABLES BY REFERENCE

                ==============================

This program shows how the address of variables can be passed to functions.

The function in this program swaps the values of two integer variables.

Programmer: Dogan Ibrahim

File:       SWAP.C

Date:       May, 2007

***************************************************************/

/* Function to swap two integers */

void Swap(int *a, int *b) {

 int temp;

 temp = *a; // Store a in temp

 *a = *b;   // Copy b to a

 *b = temp; // Copy temp to b

}

/* Start of the main program */

void main() {

 int p, q;

 p = 10;     // Set p = 10

 q = 20;     // Set q = 20

 swap(p, q); // Swap p and q (p=20, q=10)

}

Figure 4.11: Passing variables by reference to a function

4.1.4 Variable Number of Arguments

The ellipsis character (“...”) consists of three successive periods with no spaces between them. An ellipsis can be used in the argument lists of function prototypes to indicate a variable number of arguments or arguments with varying types. An example of a declaration of a function prototype with ellipsis follows. In this declaration, when the function is called we must supply at least two integer type arguments, and we can also supply any number of additional arguments:

unsigned char MyFunc(int a, int b, ...);

The header file stdarg.h must be included at the beginning of a program that uses a variable number of arguments. This header file defines a new data type called va_list, which is essentially a character pointer. In addition, macro va_start() initializes an object of type va_list to point to the address of the first additional argument presented to the function. To extract the arguments, successive calls to the macro va_arg() must be made, with the character pointer and the type of the parameter as the arguments of va_arg().

An example program is given in Figure 4.12. In this program the function header declares only one parameter of type int, and an ellipsis is used to declare a variable number of parameters. Variable num is the argument count passed by the calling program. The arguments are read by using the macro va_arg(ap, int) and then summed using variable temp and returned by the function.

/*********************************************************************

                PASSING VARIABLE NUMBER OF ARGUMENTS

               ======================================

This program shows how variable number of arguments can be passed to a

function. The function header declares one integer variable and an ellipsis is

used to declare variable number of parameters. The function adds all the

arguments and returns the sum as an integer. The number of arguments is

supplied by the calling program as the first argument to the function.

Programmer: Dogan Ibrahim

File:       VARIABLE.C

Date:       May, 2007

***********************************************************************/

#include <stdarg.h>

/* Function with variable number of parameters */

int Sum(int num, ...) {

 unsigned char j;

 va_list ap;

 int temp = 0;

 va_start(ap, num);

 for (j = 0; j < num; j++) {

  temp = temp + va_arg(ap, int);

 }

 va_end(ap);

 return temp;

}

/* Start of the main program */

void main() {

 int p;

 p = Sum(2, 3, 5);    // 2 arguments. p=3+5=8

 p = Sum(3, 2, 5, 6); // 3 arguments, p=2+5+6=13

}

Figure 4.12: Passing variable number of arguments to a function

4.1.5 Function Reentrancy

The mikroC compiler supports only a limited function reentrancy. Functions that have no arguments and local variables can be called both from the interrupt service routines and from the main program. Functions that have arguments and/or local variables can only be called from the interrupt service routines or from the main program.

4.1.6 Static Function Variables

Normally, variables declared at the beginning of a program, before the main program, are global, and their values can be accessed and modified by all parts of the program. Declaring a variable used in a function as global ensures that its value is retained from one call of the function to another, but this also undermines the variable’s privacy and reduces the portability of the function to other applications. A better approach is to declare such variables as static. Static variables are mainly used in function definitions. When a variable is declared as static, its value is retained from one call of the function to another. In the example code that follows, variable k is declared as static and initialized to zero. This variable is then incremented before exiting from the function, and the value of k remains in existence and holds its last value on the next call to the function (i.e., on the second call to the function the value of k will be 1):

void Cnt(void) {

 static int k = 0; // Declare k as static

 ..................

 ..................

 k++; // increment k